溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java如何生成和解析XML格式文件和字符串

發(fā)布時間:2020-12-08 12:27:16 來源:億速云 閱讀:208 作者:小新 欄目:編程語言

小編給大家分享一下Java如何生成和解析XML格式文件和字符串,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1、基礎(chǔ)知識:
Java解析XML一般有四種方法:DOM、SAX、JDOM、DOM4J。

2、使用介紹
1)、DOM
(1)簡介

由W3C(org.w3c.dom)提供的接口,它將整個XML文檔讀入內(nèi)存,構(gòu)建一個DOM樹來對各個節(jié)點(Node)進行操作。優(yōu)點就是整個文檔都一直在內(nèi)存中,我們可以隨時訪問任何節(jié)點,并且對樹的遍歷也是比較熟悉的操作;缺點則是耗內(nèi)存,并且必須等到所有的文檔都讀入內(nèi)存才能進行處理。

(2)示例代碼:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <TelePhone>
        <type name="nokia">
            <price>599</price>
            <operator>CMCC</operator>
        </type>
        <type name="xiaomi">
            <price>699</price>
            <operator>ChinaNet</operator>
        </type>
    </TelePhone>
</root>
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLHandler {
    public XMLHandler(){
        
    }
    
    public String createXML(){
        String xmlStr = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            document.setXmlVersion("1.0");
            
            Element root = document.createElement("root");
            document.appendChild(root);
            
            Element telephone = document.createElement("TelePhone");
            
            Element nokia = document.createElement("type");
            nokia.setAttribute("name", "nokia");
            
            Element priceNokia = document.createElement("price");
            priceNokia.setTextContent("599");
            nokia.appendChild(priceNokia);
            
            Element operatorNokia = document.createElement("operator");
            operatorNokia.setTextContent("CMCC");
            nokia.appendChild(operatorNokia);
            
            telephone.appendChild(nokia);
            
            Element xiaomi = document.createElement("type");
            xiaomi.setAttribute("name", "xiaomi");
            
            Element priceXiaoMi = document.createElement("price");
            priceXiaoMi.setTextContent("699");
            xiaomi.appendChild(priceXiaoMi);
            
            Element operatorXiaoMi = document.createElement("operator");
            operatorXiaoMi.setTextContent("ChinaNet");
            xiaomi.appendChild(operatorXiaoMi);
            
            telephone.appendChild(xiaomi);
            
            root.appendChild(telephone);
            
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transFormer = transFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            
            //export string
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            transFormer.transform(domSource, new StreamResult(bos));
            xmlStr = bos.toString();
            
            //-------
            //save as file
            File file = new File("TelePhone.xml");
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            StreamResult xmlResult = new StreamResult(out);
            transFormer.transform(domSource, xmlResult);
            //--------
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return xmlStr;
    }
    
    public void parserXML(String strXML){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader sr = new StringReader(strXML);
            InputSource is = new InputSource(sr);
            Document doc = builder.parse(is);
            Element rootElement = doc.getDocumentElement();
            NodeList phones = rootElement.getElementsByTagName("type");
            for (int i = 0; i < phones.getLength(); i++) {
                Node type = phones.item(i);
                String phoneName = ((Element)type).getAttribute("name");
                System.out.println("Phone name = "+phoneName);
                NodeList properties = type.getChildNodes();
                for (int j = 0; j < properties.getLength(); j++) {
                    Node property = properties.item(j);
                    String nodeName = property.getNodeName();
                    if (nodeName.equals("price")) {
                        String price=property.getFirstChild().getNodeValue();
                        System.out.println("price="+price);
                    } else if (nodeName.equals("operator")) {
                        String operator=property.getFirstChild().getNodeValue();
                        System.out.println("operator="+operator);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        XMLHandler handler = new XMLHandler();
        String xml = handler.createXML();
        System.out.println(xml);
        handler.parserXML(xml);
    }
}

(3)元素(Element)和結(jié)點(Node)的區(qū)別(org.w3c.dom)
Node對象是整個文檔對象模型的主要數(shù)據(jù)類型,是DOM中最基本的對象,代表了文檔樹中的抽象節(jié)點。但在實際使用中很少會直接使用Node對象,而是使用Node對象的子對象Element,Attr,Text等。
Element對象表示HTML或XML文檔中的一個元素,是Node類最主要的子對象,在元素中可以包含屬性,因而Element中有存取其屬性的方法。
Element是從Node繼承而來的,元素是一個小范圍的定義,必須是含有完整信息的結(jié)點才是一個元素,例如<div>...</div>。但是一個結(jié)點不一定是一個元素,而一個元素一定是一個結(jié)點。
node有幾個子類型:Element,Text,Attribute,RootElement,Comment,Namespace等

2)、SAX


3)、JDOM

4)、DOM4J
(1)簡介
dom4j是目前在xml解析方面是最優(yōu)秀的(Hibernate、Sun的JAXM也都使用dom4j來解析XML),它合并了許多超出基本XML文檔表示的功能,包括集成的XPath支持、XML Schema支持以及用于大文檔或流化文檔的基于事件的處理。
在使用XPATH時要增加jaxen.jar,否則會出現(xiàn)如下錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)      
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)

(2)示例代碼:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.InputSource;
public class XMLHandler {
    public XMLHandler() {
        // TODO Auto-generated constructor stub
    }
    
    public String createXML(){
        String strXML = null;
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root");
        
        Element phone = root.addElement("TelePhone");
        
        Element nokia = phone.addElement("type");
        nokia.addAttribute("name", "nokia");
        Element price_nokia = nokia.addElement("price");
        price_nokia.addText("599");
        Element operator_nokia = nokia.addElement("operator");
        operator_nokia.addText("CMCC");
        
        Element xiaomi = phone.addElement("type");
        xiaomi.addAttribute("name", "xiaomi");
        Element price_xiaomi = xiaomi.addElement("price");
        price_xiaomi.addText("699");
        Element operator_xiaomi = xiaomi.addElement("operator");
        operator_xiaomi.addText("ChinaNet");
        
        //--------
        StringWriter strWtr = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter xmlWriter =new XMLWriter(strWtr, format);
        try {
            xmlWriter.write(document);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        strXML = strWtr.toString();
        //--------
        
        //-------
        //strXML=document.asXML();
        //------
        
        //-------------
        File file = new File("TelePhone.xml");  
        if (file.exists()) {  
            file.delete();  
        }  
        try {
            file.createNewFile();
            XMLWriter out = new XMLWriter(new FileWriter(file));  
            out.write(document);  
            out.flush();  
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //--------------
        
        return strXML;
    }
    
    public void parserXML(String strXML){
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(strXML);
        InputSource is = new InputSource(sr);
        try {
            Document document = reader.read(is);
            
            Element root = document.getRootElement();
            
            //get element
            List<Element> phoneList = root.elements("TelePhone");
            List<Element> typeList = phoneList.get(0).elements("type");
            for (int i=0;i<typeList.size();i++){
                Element element = typeList.get(i);
                String phoneName = element.attributeValue("name");
                System.out.println("phonename = "+phoneName);
                //get all element
                List<Element> childList = element.elements();
                for (int j=0;j<childList.size();j++){
                    Element e = childList.get(j);
                    System.out.println(e.getName()+"="+e.getText());
                }
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void parserXMLbyXPath(String strXML){
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(strXML);
        InputSource is = new InputSource(sr);
        try {
            Document document = reader.read(is);
            List list = document.selectNodes("/root/TelePhone/type");
            for(int i=0;i<list.size();i++){
                Element e = (Element) list.get(i);
                System.out.println("phonename="+e.attributeValue("name"));
                List list1 = e.selectNodes("./*");
                for(int j=0;j<list1.size();j++){
                    Element e1 = (Element) list1.get(j);
                    System.out.println(e1.getName()+"="+e1.getText());
                }
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        XMLHandler handler = new XMLHandler();
        String strXML=handler.createXML();
        System.out.println(strXML);
        handler.parserXML(strXML);
        System.out.println("-----------");
        handler.parserXMLbyXPath(strXML);
    }
}

5)XPATH
(1)簡介
XPath是一門在XML文檔中查找信息的語言。XPath用于在XML文檔中通過元素和屬性進行導航。
具體語法介紹參考:http://w3school.com.cn/xpath/index.asp

(2)示例代碼:

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLHandler {
    public XMLHandler() {
        // TODO Auto-generated constructor stub
    }
    
    public void parserXML(String strXML){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader sr = new StringReader(strXML);
            InputSource is = new InputSource(sr);
            Document doc = builder.parse(is);
            
            XPathFactory xFactory = XPathFactory.newInstance();
            XPath xpath = xFactory.newXPath();
            XPathExpression expr = xpath.compile("/root/TelePhone/type");
            NodeList phones = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);  
            for (int i = 0; i < phones.getLength(); i++) {
                Node type = phones.item(i);
                String phoneName = ((Element)type).getAttribute("name");
                System.out.println("Phone name = "+phoneName);
                XPathExpression expr1 = xpath.compile("./*");
                NodeList list = (NodeList) expr1.evaluate(type, XPathConstants.NODESET);
                for(int j =0;j<list.getLength();j++){
                    Element e1 = (Element) list.item(j);
                    System.out.println(e1.getNodeName()+"="+e1.getTextContent());
                }
                
            }
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String strXML="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"+
                    "<root>"+
                        "<TelePhone>"+
                            "<type name=\"nokia\">"+
                                "<price>599</price>"+
                                "<operator>CMCC</operator>"+
                            "</type>"+
                            "<type name=\"xiaomi\">"+
                                "<price>699</price>"+
                                "<operator>ChinaNet</operator>"+
                            "</type>"+
                        "</TelePhone>"+
                    "</root>";
        XMLHandler handler = new XMLHandler();
        handler.parserXML(strXML);
    }
}

以上是“Java如何生成和解析XML格式文件和字符串”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI