码迷,mamicode.com
首页 > 其他好文 > 详细

JAXP解析xml文件

时间:2015-05-10 22:07:49      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookshelf> <book> <name bookname="thinkinjava">Think in Java</name> <author>Bruce Eckel</author> <price>95.00</price> </book> <book> <name>Effective Java</name> <author>Joshua Bloch</author> <price>52.00</price> </book> </bookshelf>
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

public class JaxpDemo {

    public static void main(String[] args) {

        // 创建工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            // 得到dom解析器
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 解析xml文档,得到Document对象
            Document document = builder.parse("day02/src/book.xml");

            // 对xml文档进行CRUD

            //
            NodeList nodeList = document.getElementsByTagName("name");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                String content = node.getTextContent();
                System.out.println(content);
            }

            // 遍历xml节点TagName
            Node root = document.getElementsByTagName("bookshelf").item(0);
            list(root);

            // 获取标签属性的值
            Element bookname = (Element) document.getElementsByTagName("name").item(0);
            String value = bookname.getAttribute("bookname");
            System.out.println(value);

            //// 增加节点
            Element version = document.createElement("version");
            version.setTextContent("2");
            // 选择指定的插入节点位置
            Element price = (Element) document.getElementsByTagName("price").item(0);
            // 选择增加的节点
            Element book1 = (Element) document.getElementsByTagName("book").item(0);
            /*book1.appendChild(element);*/
            book1.insertBefore(version, price);

            // 标签增加属性
            Element book2 = (Element) document.getElementsByTagName("name").item(1);
            book2.setAttribute("bookname", "effectivejava");

            //
            Element price1 = (Element) document.getElementsByTagName("price").item(0);
            book1.removeChild(price1);
            /*price1.getParentNode().removeChild(price1);*/
       // Element author = (Element) document.getElementsByTagName("author").item(0); author.setTextContent("Bruce Eckel Jr."); // 更新到文件 TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day02/src/book.xml"))); } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) { e.printStackTrace(); } } public static void list(Node node) { if (node instanceof Element) { System.out.println(node.getNodeName()); } NodeList childList = node.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node child = childList.item(i); list(child); } } }

 

JAXP解析xml文件

标签:

原文地址:http://www.cnblogs.com/geb515/p/4492925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!