/////////////////////////////////jaxp对xml文档进行解析///////////////////////////////////////////

要操作的xml文件

<?xml version="1.0" encoding="GB2312" standalone="no"?><PhoneInfo>
    <Brand name="华为">
        <Type name="U8650"/><Type name="HW123"/>
        <Type name="HW321"/>
    </Brand>
    <Brand name="苹果">
    <Type name="iPhone4"/>
    </Brand>
    <Brand name="vivo">
        <Type name="nb"/>
    </Brand>
</PhoneInfo>

//////////////////////////////////////////////////////////////////////////////////////////////////

package xinhuiji_day15;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class xmlTest {

/**
     * @param args
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
//        test3();
//        findId("3");
        addNode();
    }
  //查找节点的内容
    public static void test3() throws ParserConfigurationException,
            SAXException, IOException {
        DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = daoFactory.newDocumentBuilder();
        Document doc = builder.parse("src/网易手机各地行情.xml");
        NodeList nodeList = doc.getElementsByTagName("pubDate");
        for(int i = 0;i<nodeList.getLength();i++){
            Element e = (Element) nodeList.item(i);
            System.out.println(e.getFirstChild().getNodeValue());
        }
    }
    
    public void test() throws ParserConfigurationException, SAXException, IOException{
        DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = daoFactory.newDocumentBuilder();
        Document doc = builder.parse("src/收藏信息.xml");
        NodeList nodeList = doc.getElementsByTagName("Brand");
        for(int i = 0;i<nodeList.getLength();i++){
            Element e = (Element) nodeList.item(i);
//            System.out.println(e.getFirstChild().getNodeValue());
            System.out.println(e.getTagName());
            System.out.println(e.getAttribute("name"));
            NodeList nod = e.getElementsByTagName("Type");
            for(int j = 0;j<nod.getLength();j++){
                Element ne = (Element) nod.item(j);
                System.out.println("\t"+ne.getAttribute("name"));
            }
        }
    }
    
    public static void findId(String id) throws ParserConfigurationException, SAXException, IOException{
        DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = daoFactory.newDocumentBuilder();
        Document doc = builder.parse("src/网易手机各地行情.xml");
        NodeList nodeList = doc.getElementsByTagName("item");
        for(int i = 0;i<nodeList.getLength();i++){
            Element e = (Element) nodeList.item(i);
            String xmlId = e.getAttribute("id");
            if(xmlId.equals(id)){
                System.out.println(e.getElementsByTagName("title").item(0)
                        .getFirstChild().getNodeValue());
            }
        }
    }
    //增加节点
    public static void addNode() throws ParserConfigurationException, SAXException, IOException, TransformerException{
        DocumentBuilderFactory daoFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = daoFactory.newDocumentBuilder();
        Document doc = builder.parse("src/收藏信息.xml");
        Element brandElement = doc.createElement("Brand");
        brandElement.setAttribute("name", "vivo");
        Element typeElement = doc.createElement("Type");
        typeElement.setAttribute("name", "nb");
        Element rootElement = (Element) doc.getElementsByTagName("PhoneInfo").item(0);
        brandElement.appendChild(typeElement);
        rootElement.appendChild(brandElement);
        //向xml文件中写
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer ts = tff.newTransformer();
        DOMSource source = new DOMSource(doc);
        ts.setOutputProperty(OutputKeys.ENCODING, "gb2312");
        StreamResult result = new StreamResult(new File("src/收藏信息.xml"));
        ts.transform(source, result);
        
        
    }

}

xml 操作的更多相关文章

  1. LINQ系列:LINQ to XML操作

    LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...

  2. T-Sql(五)xml操作

    t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...

  3. XML格式示例 与 XML操作(读取)类封装

    header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...

  4. 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】

    一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...

  5. 简单的XML操作类

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  6. .net学习笔记---xml操作及读写

    一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...

  7. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  8. php xml 操作。

    参考 文章:http://www.cnblogs.com/zcy_soft/archive/2011/01/26/1945482.html DOMDocument相关的内容. 属性: Attribut ...

  9. XML Helper XML操作类

    写的一个XML操作类,包括读取/插入/修改/删除. using System;using System.Data;using System.Configuration;using System.Web ...

  10. 我的PHP之旅--XML操作

    XML操作 XML主要是做数据存储和WEB服务的,所以我们难免要操作它,这里只介绍PHP的simpleXML方式. 我们要操作的XML: <?xml version="1.0" ...

随机推荐

  1. 洛谷 [P2341] 受欢迎的牛

    强连通分量 一个结论: 在有向图中, 一个联通块能被所有点遍历当且仅当图中只有一个连通块出度为零 #include <iostream> #include <cstdio> # ...

  2. query带进度上传插件Uploadify(ASP.NET版本)使用

    原文发布时间为:2010-05-13 -- 来源于本人的百度文章 [由搬家工具导入] 本文将带给大家很帅的jquery上传插件,ASP.NET版本的哦,这个插件是Uploadify实现的效果非常不错, ...

  3. SQL语句效率问题的几点总结

    原文发布时间为:2009-10-29 -- 来源于本人的百度文章 [由搬家工具导入] 1. SQL优化的原则是: 将一次操作需要读取的BLOCK数减到最低,即在最短的时间达到最大的数据吞吐量。   调 ...

  4. C/C++初学攻略

    最近有朋友问我C++(or C)怎么入门,其实这个还真不是很好回答的,想了下就写下这篇博文以说下我自己的学习路程吧! 正儿八经的,其实我觉得自己也学得不咋地,不管是C还是C++都是如此的强大,要真正的 ...

  5. 给所有ajax请求增加随机数

    var origionAjax=$.ajax $.ajax=function(obj){ obj.url=(obj.url).indexOf("?")>-1?(obj.url ...

  6. [LeetCode] Repeated DNA Sequences hash map

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. [LeetCode] Populating Next Right Pointers in Each Node 深度搜索

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. delphi 内存泄露 分析

  9. HDU - 2970 Suffix reconstruction

    Discription Given a text s[1..n] of length n, we create its suffix array by taking all its suffixes: ...

  10. 批处理备份mysql数据

    客户服务器,需要每天定时备份数据库,没办法,bat走起! 代码如下: @echo off C: cd C:\***\***\mysql\bin set Ymd=%date:~,4%%date:~5,2 ...