/////////////////////////////////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. BZOJ 4766: 文艺计算姬

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 456  Solved: 239[Submit][Status][Discuss] ...

  2. SQl性能优化1

    原文发布时间为:2010-11-05 -- 来源于本人的百度文章 [由搬家工具导入] 性能优化,从上面我说的两点来考虑优化,主要是以Sql为主,平台暂不介绍,我们现在使用的数据库以SQL Server ...

  3. ThreadPool学习草稿1

    原文发布时间为:2010-10-27 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Collections.Generic;using Syste ...

  4. delphi 窗体透明详解TransparentColorValue,窗体透明控件不透明

    关于窗体透明的做法 来自:http://blog.csdn.net/shuaihj/article/details/8610343 关于窗体透明的做法 1.在Delphi中,设置窗体的AlphaBle ...

  5. GIT 的常规操作

    GIT 的常规操作 常规操作也是我自己平时常用的几个命令, 学自于 pro git 这本书中 git 配置文件 git的配置文件位置 针对所有用户:/etc/gitconfig 针对当前用户: -/. ...

  6. html5---音频视频基础一

    //html5 音频和视频 :标签 a: audio,video b: source :视频容器 a:容器文件,类似于压缩了一组文件 -音频轨道 -视频轨道 -元数据:封面,标题,字幕等 -格式:.a ...

  7. Codeforces Gym100814 F.Geometry (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)

    这个题真的是超级超级水啊,哈哈哈哈哈哈.不要被题面吓到,emnnn,就这样... 代码: 1 #include<iostream> 2 #include<cstring> 3 ...

  8. K皇后问题递归解法

      #include<iostream> #include<cmath> #include<ctime> using namespace std; bool che ...

  9. 洛谷——P2781 传教

    P2781 传教 题目背景 写完暑假作业后,bx2k去找pear玩.pear表示他要去汉中传教,于是bx2k准备跟着去围观. 题目描述 pear把即将接受传教的人排成一行,每个人从左到右的编号为1-n ...

  10. Java线程池ThreadPoolExecutor类源码分析

    前面我们在java线程池ThreadPoolExecutor类使用详解中对ThreadPoolExector线程池类的使用进行了详细阐述,这篇文章我们对其具体的源码进行一下分析和总结: 首先我们看下T ...