Java中使用DOM方式解析和创建XML文档、及dom4j使用简介

1.解析XML文档
Java代码

package peixun;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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;

public class ReadXML {

    public static void main(String[] args) {

        try {
            //DOM方式解析XML文档
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//调用DocumentBuilderFactory类,使用DocumentBuilderFactory的静态方法获取一个新的实例
            DocumentBuilder builder = factory.newDocumentBuilder();
            
            Document document = builder.parse(new File("languages.xml"));
            
            Element root = document.getDocumentElement();
            System.out.println("cat="+root.getAttribute("cat"));
            NodeList list = root.getElementsByTagName("lan");
            
            for (int i = 0; i < list.getLength(); i++) {
                Element lan = (Element) list.item(i);
                System.out.println("-------------------");
                System.out.println("id="+lan.getAttribute("id"));
                
                NodeList clist = lan.getChildNodes();
                for (int j = 0; j < clist.getLength(); j++) {
                    Node c = clist.item(j);
//c instanceof Element是为了不输出不可见的字节
                    if (c instanceof Element) {
                        System.out.println(c.getNodeName()+"="+c.getTextContent());
                    }
                    
                }
                
            }
            
            }
            catch (SAXException e) {
                e.printStackTrace();
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        
    }

}

XML文档



    
        Java
        Eclipse
    
    
        Swift
        Xcode
    
    
        C#
        Visual Studio
    

2.创建XML文档
Java代码:

package peixun;

import java.io.File;
import java.io.StringWriter;
import java.nio.file.Path;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;

public class CreateXML {

    public static void main(String[] args) {
        try {
            
            
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            
            Document document = builder.newDocument();
            Element root = document.createElement("Languages");
            root.setAttribute("cat", "it");
            
            Element lan1 = document.createElement("lan");
            lan1.setAttribute("id", "1");
            Element name1 = document.createElement("name");
            name1.setTextContent("Swift");
            Element ide1 = document.createElement("ide");
            ide1.setTextContent("Xcode");
            //name和ide添加到lan的子节点
            lan1.appendChild(name1);
            lan1.appendChild(ide1);
            //lan节点添加到root节点下
            root.appendChild(lan1);
            //root添加到document中
            document.appendChild(root);
            
            Element lan2 = document.createElement("lan");
            lan2.setAttribute("id", "2");
            Element name2 = document.createElement("name");
            name2.setTextContent("C#");
            Element ide2 = document.createElement("ide");
            ide2.setTextContent("Visual Studio");
            //name和ide添加到lan的子节点
            lan1.appendChild(name2);
            lan1.appendChild(ide2);
            //lan节点添加到root节点下
            root.appendChild(lan2);
            
            Element lan3 = document.createElement("lan");
            lan3.setAttribute("id", "3");
            Element name3 = document.createElement("name");
            name3.setTextContent("Java");
            Element ide3 = document.createElement("ide");
            ide3.setTextContent("eclipse");
            //name和ide添加到lan的子节点
            lan1.appendChild(name3);
            lan1.appendChild(ide3);
            //lan节点添加到root节点下
            root.appendChild(lan3);
            
            //----------
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            //transformer将XML文档转化成其他格式
            Transformer transformer = transformerFactory.newTransformer();
            StringWriter writer = new StringWriter();
            
            //字符流形式输出
            transformer.transform(new DOMSource(document),new StreamResult(writer));
            
            System.out.println(writer.toString());
            //以文件形式输出
            transformer.transform(new DOMSource(document),new StreamResult(new File("newxml.xml")));
            
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

}

3.dom4j使用简介

package peixun;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;


public class Testdom4j {

    public static void main(String[] args) {

        try {
            String xmlString = "Sai";
            //把xmlString字符串转换成document对象
            Document document = DocumentHelper.parseText(xmlString);
            //将document对象输出成字符串
            System.out.println(document.asXML());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        
    }

}

你可能感兴趣的:(Java中使用DOM方式解析和创建XML文档、及dom4j使用简介)