国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 java類庫 Java Basics Series 17: Detailed explanation of using DOM, SAX, JDOM, and DOM4J to parse XML files

Java Basics Series 17: Detailed explanation of using DOM, SAX, JDOM, and DOM4J to parse XML files

Oct 13, 2016 pm 03:57 PM

1 Introduction

In Java, there are many ways to parse XML files, among which the four most common methods are probably DOM, SAX, JDOM, and DOM4J. Among them, DOM and SAX, two ways of parsing XML files, have jdk's own API, so there is no need to introduce additional third-party jar packages. On the contrary, both JDOM and DOM4J parsing methods are third-party open source projects, so when using these two methods to parse XML files, you need to introduce additional related jar packages

(1) DOM

DOM is used with The official W3C standard for representing XML documents in a platform- and language-independent way. The DOM is a collection of nodes or pieces of information organized in a hierarchical structure that allows developers to find specific information in the tree. Analyzing this structure usually requires loading the entire document and constructing the hierarchy before any work can be done. Therefore, when using DOM to parse XML files, the parser needs to read the entire XML file into memory to form a tree structure. Convenient for subsequent operations

Advantages: The entire document tree is in memory, easy to operate; supports deletion, modification, rearrangement and other operations

Disadvantages: Transferring the entire document into memory (including useless nodes) is a waste of time and memory , if the XML is too large, it is easy to have a memory overflow problem

(2) SAX

Since the DOM needs to read the entire file at once when parsing the XML file, there are many shortcomings when the file is too large, so in order to solve this problem, there is With SAX, an event-driven parsing method,

SAX parses XML files by continuously loading content into memory from top to bottom. When the parser finds the start mark, end mark, text, document start mark, document When the end flag and other related flags are used, some corresponding events will be triggered. All we need to do is to write custom code in the methods of these events to save the obtained data

Advantages: There is no need to load the entire document in advance. Occupies less resources (memory); the code written using SAX parsing is less than the code written using DOM parsing

Disadvantages: not persistent; after the event, if the data is not saved, the data is lost; stateless; from Only text can be obtained in the event, but I don’t know which element the text belongs to

(3) JDOM

Using JDOM to parse XML files is similar to using DOM to parse. From the code point of view, the parsing ideas are similar. JDOM differs from DOM in two main ways: First, JDOM only uses concrete classes instead of interfaces, which simplifies the API in some aspects, but also limits flexibility. Secondly, JDOM’s API makes extensive use of Collections classes, simplifying the use of Java developers who are already familiar with these classes. Advantages: open source projects; easier to understand than DOM. Disadvantages: JDOM itself does not contain a parser. It usually uses a SAX2 parser to parse and validate input XML documents

(4) DOM4J

DOM4J is a very, very excellent Java XML API with excellent performance, powerful functions and extreme ease of use. It is also a Open source software. Nowadays you can see that more and more Java software is using DOM4J to read and write XML

Because DOM4J is very powerful in terms of performance and code writing, especially when the XML file is large, use DOM4J to parse it There will also be higher efficiency. Therefore, it is recommended that if you need to parse XML files, you can consider using DOM4J to parse them as much as possible. Of course, if the file is very small, it is also possible to use DOM to parse it

Advantages:

Open source project

DOM4J is an intelligent branch of JDOM, which incorporates functions that require more than basic XML documents

Excellent performance and flexibility Good, easy to use and other features

Second DOM parsing XML files

(1) Before writing and testing the code, you need to prepare an XML file. The file I prepared here is: demo1.xml

demo1.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<employees>
    <user id="1">
        <name>zifangsky</name>
        <age>10</age>
        <sex>male</sex>
        <contact>https://www.zifangsky.cn</contact>
    </user>
    <user id="2">
        <name>admin</name>
        <age>20</age>
        <sex>male</sex>
        <contact>https://www.tar.pub</contact>
    </user>
</employees>

(2) Code example:

package cn.zifangsky.xml;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public class DomParseTest {
 
    public static void main(String[] args) {
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
            // 加載一個xml文件
            Document document = dBuilder
                    .parse("src/cn/zifangsky/xml/demo1.xml");
            // 獲取user節(jié)點集合
            NodeList userList = document.getElementsByTagName("user");
            int userListLength = userList.getLength();
            System.out.println("此xml文件一共有" + userListLength + "個&#39;user&#39;節(jié)點\n");
 
            // 遍歷
            for (int i = 0; i < userListLength; i++) {
                // 通過item方法獲取指定的節(jié)點
                Node userNode = userList.item(i);
 
                // *********************解析屬性***********************
 
                // 獲取該節(jié)點的所有屬性值,如:id="1"
                NamedNodeMap userAttributes = userNode.getAttributes();
                System.out.println("&#39;user&#39;節(jié)點" + i + "有"
                        + userAttributes.getLength() + "個屬性:");
                /**
                 * 1 在不清楚有哪些屬性的情況下可以遍歷所有屬性,
                 * 并獲取每個屬性對應的屬性名和屬性值
                 * */
                for (int j = 0; j < userAttributes.getLength(); j++) {
                    // &#39;user&#39;節(jié)點的每個屬性組成的節(jié)點
                    Node attrnNode = userAttributes.item(j);
                    System.out.println("屬性" + j + ": 屬性名: "
                            + attrnNode.getNodeName() + " ,屬性值: "
                            + attrnNode.getNodeValue());
                }
                /**
                 * 2 在知道有哪些屬性值的情況下,可以獲取指定屬性名的屬性值
                 * */
                Element userElement = (Element) userList.item(i);
                System.out.println("屬性為&#39;id&#39;的對應值是: "
                        + userElement.getAttribute("id"));
 
                // *********************解析子節(jié)點************************
                NodeList childNodes = userNode.getChildNodes();
                System.out.println("\n該節(jié)點一共有" + childNodes.getLength()
                        + "個子節(jié)點,分別是:");
 
                // 遍歷子節(jié)點
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node childNode = childNodes.item(k);
                    // 從輸出結果可以看出,每行后面的換行符也被當做了一個節(jié)點,因此是:4+5=9個子節(jié)點
                    // System.out.println("節(jié)點名: " + childNode.getNodeName() +
                    // ",節(jié)點值: " + childNode.getTextContent());
                    // 僅取出子節(jié)點中的&#39;ELEMENT_NODE&#39;,換行符組成的Node是&#39;TEXT_NODE&#39;
                    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                        // System.out.println("節(jié)點名: " + childNode.getNodeName()
                        // + ",節(jié)點值: " + childNode.getTextContent());
                        // 最低一層是文本節(jié)點,節(jié)點名是&#39;#text&#39;
                        System.out.println("節(jié)點名: " + childNode.getNodeName()
                                + ",節(jié)點值: "
                                + childNode.getFirstChild().getNodeValue());
                    }
                }
 
                System.out.println("***************************");
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}

As can be seen from the above code, when using DOM to parse XML files, you generally need to do the following steps:

Create a Document Builder Factory (DocumentBuilderFactory) instance

Through the above The DocumentBuilderFactory generates a new document builder (DocumentBuilder)

Use the above DocumentBuilder to parse (parse) an XML file and generate a document tree (Document)

Get the node with the specified id through the Document or get all the qualified nodes based on the node name The node set

traverses each node, and you can obtain the attributes, attribute values ??and other related parameters of the node

If the node still has child nodes, you can continue to traverse all its child nodes according to the above method

(3) above The code output is as follows:

此xml文件一共有2個&#39;user&#39;節(jié)點
 
&#39;user&#39;節(jié)點0有1個屬性:
屬性0: 屬性名: id ,屬性值: 1
屬性為&#39;id&#39;的對應值是: 1
 
該節(jié)點一共有9個子節(jié)點,分別是:
節(jié)點名: name,節(jié)點值: zifangsky
節(jié)點名: age,節(jié)點值: 10
節(jié)點名: sex,節(jié)點值: male
節(jié)點名: contact,節(jié)點值: https://www.zifangsky.cn
***************************
&#39;user&#39;節(jié)點1有1個屬性:
屬性0: 屬性名: id ,屬性值: 2
屬性為&#39;id&#39;的對應值是: 2
 
該節(jié)點一共有9個子節(jié)點,分別是:
節(jié)點名: name,節(jié)點值: admin
節(jié)點名: age,節(jié)點值: 20
節(jié)點名: sex,節(jié)點值: male
節(jié)點名: contact,節(jié)點值: https://www.tar.pub
***************************

Three SAX parsed XML files

在進行本次測試時,并不引入其他XML文件,仍然使用上面的demo1.xml文件

由于SAX解析XML文件跟DOM不同,它并不是將整個文檔都載入到內存中。解析器在解析XML文件時,通過逐步載入文檔,從上往下一行行的解析XML文件,在碰到文檔開始標志、節(jié)點開始標志、文本文檔、節(jié)點結束標志、文檔結束標志時進行對應的事件處理。因此,我們首先需要構造一個這樣的解析處理器來申明:當解析到這些標志時,我們需要進行怎樣的自定義處理

(1)解析處理器SAXParseHandler.java:

package cn.zifangsky.xml;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class SAXParseHandler extends DefaultHandler {
 
    /**
     * 用來遍歷XML文件的開始標簽
     * */
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
         
        //解析&#39;user&#39;元素的屬性值
//      if(qName.equals("user"))
//          System.out.println("&#39;user&#39;元素的id屬性值是:" + attributes.getValue("id"));
         
        //遍歷并打印元素的屬性
        int length = attributes.getLength();
        if(length > 0){
            System.out.println("元素&#39;" + qName + "&#39;的屬性是:");
             
            for(int i=0;i<length;i++){
                System.out.println("    屬性名:" + attributes.getQName(i) + ",屬性值: " + attributes.getValue(i));
            }
            System.out.println();
        }
         
        System.out.print("<" + qName + ">");
 
    }
 
    /**
     * 用來遍歷XML文件的結束標簽
     * */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(uri, localName, qName);
         
        System.out.println("<" + qName + "/>");
    }
     
    /**
     * 文本內容
     * */
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        String value = new String(ch, start, length).trim();
        if(!value.equals(""))
            System.out.print(value);
    }
 
    /**
     * 用來標識解析開始
     * */
    @Override
    public void startDocument() throws SAXException {
        System.out.println("SAX解析開始");
        super.startDocument();
    }
     
    /**
     * 用來標識解析結束
     * */
    @Override
    public void endDocument() throws SAXException {
        System.out.println("SAX解析結束");
        super.endDocument();
    }
 
}

關于上面代碼的一些含義我這里就不再做解釋了,可以自行參考注釋內容

(2)測試:

SAXParseTest.java文件:

package cn.zifangsky.xml;
 
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
public class SAXParseTest {
 
    public static void main(String[] args) {
        SAXParserFactory sFactory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = sFactory.newSAXParser();
            //創(chuàng)建自定義的SAXParseHandler解析類
            SAXParseHandler saxParseHandler = new SAXParseHandler();
            saxParser.parse("src/cn/zifangsky/xml/demo1.xml", saxParseHandler);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

從上面的代碼可以看出,使用SAX解析XML文件時,一共傳遞進去了兩個參數(shù),分別是:XML文件路徑和前面定義的解析處理器。有了具體的XML文件以及對應的處理器來處理對應的標志事情,因此SAX這種解析方式就可以順利地進行解析工作了

(3)上面測試的輸出如下:

SAX解析開始
<employees>元素&#39;user&#39;的屬性是:
    屬性名:id,屬性值: 1
 
<user><name>zifangsky<name/>
<age>10<age/>
<sex>male<sex/>
<contact>https://www.zifangsky.cn<contact/>
<user/>
元素&#39;user&#39;的屬性是:
    屬性名:id,屬性值: 2
 
<user><name>admin<name/>
<age>20<age/>
<sex>male<sex/>
<contact>https://www.tar.pub<contact/>
<user/>
<employees/>
SAX解析結束

四 JDOM解析XML文件

跟前面兩種解析方式不同的是,使用JDOM來解析XML文件需要下載額外的jar包

(1)下載jar包并導入到項目中:

下載地址:http://www.jdom.org/downloads/index.html

目前最新版本是:JDOM 2.0.6

然后將下載得到的“jdom-2.0.6.jar”文件導入到測試項目中

注:關于如何在一個Java項目中導入額外的jar,這里將不多做解釋,不太會的童鞋可以自行百度

(2)測試代碼:

JDOMTest.java:

package cn.zifangsky.xml;
 
import java.util.List;
 
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
 
public class JDOMTest {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        SAXBuilder saxBuilder = new SAXBuilder();
        try {
            Document document = saxBuilder.build("src/cn/zifangsky/xml/demo1.xml");
             
            //獲取XML文件的根節(jié)點
            Element rootElement = document.getRootElement();
//          System.out.println(rootElement.getName());
            List<Element> usersList = rootElement.getChildren();  //獲取子節(jié)點
            for(Element u : usersList){
//              List<Attribute> attributes = u.getAttributes();
//              for(Attribute attribute : attributes){
//                  System.out.println("屬性名:" + attribute.getName() + ",屬性值:" + attribute.getValue());
//              }
                System.out.println("&#39;id&#39;的值是: " + u.getAttributeValue("id"));
            }
             
             
        }catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}

從上面的代碼可以看出,使用JDOM來解析XML文件,主要需要做以下幾個步驟:

新建一個SAXBuilder

通過SAXBuilder的build方法傳入一個XML文件的路徑得到Document

通過Document的getRootElement方法獲取根節(jié)點

通過getChildren方法獲取根節(jié)點的所有子節(jié)點

然后是遍歷每個子節(jié)點,獲取屬性、屬性值、節(jié)點名、節(jié)點值等內容

如果該節(jié)點也有子節(jié)點,然后同樣可以通過getChildren方法獲取該節(jié)點的子節(jié)點

后面的步驟跟上面一樣,不斷遞歸到文本節(jié)點截止

(3)上面測試的輸出如下:

&#39;id&#39;的值是: 1
&#39;id&#39;的值是: 2

五 DOM4J解析XML文件

jar包下載地址:https://sourceforge.net/projects/dom4j/files/

同樣,在使用DOM4J解析XML文件時需要往項目中引入“dom4j-1.6.1.jar”文件

(1)一個簡單實例:

i)DOM4JTest.java:

package cn.zifangsky.xml;
 
import java.io.File;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class DOM4JTest {
 
    public static void main(String[] args) {
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("src/cn/zifangsky/xml/demo1.xml"));
            //獲取XML文件的根節(jié)點
            Element rootElement = document.getRootElement();
            System.out.println(rootElement.getName());
             
            //通過elementIterator方法獲取迭代器
            Iterator<Element> iterator = rootElement.elementIterator();
            //遍歷
            while(iterator.hasNext()){
                Element user = iterator.next();
                //獲取屬性并遍歷
                List<Attribute> aList = user.attributes();
             
                for(Attribute attribute : aList){
                    System.out.println("屬性名:" + attribute.getName() + ",屬性值:" + attribute.getValue());
                }
                 
                //子節(jié)點
                Iterator<Element> childList = user.elementIterator();
                while(childList.hasNext()){
                    Element child = childList.next();
//                  System.out.println(child.getName() + " : " + child.getTextTrim());
                    System.out.println(child.getName() + " : " + child.getStringValue());
                }
            }
         
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

從上面的代碼可以看出,跟前面的JDOM解析方式流程是差不多的,并且關鍵地方也有注釋,因此這里就不多做解釋了

ii)上面的代碼輸出如下:

employees
屬性名:id,屬性值:1
name : zifangsky
age : 10
sex : male
contact : https://www.zifangsky.cn
屬性名:id,屬性值:2
name : admin
age : 20
sex : male
contact : https://www.tar.pub

(2)將XML文件解析成Java對象:

i)為了方便測試,這里準備一個新的XML文件:

demo2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<user id="2">
    <name>zifangsky</name>
    <age>100</age>
    <sex>男</sex>
    <contact>https://www.zifangsky.cn</contact>
    <ownPet id="1">旺財</ownPet>
    <ownPet id="2">九頭貓妖</ownPet>
</user>

ii)同時準備一個Java實體類,恰好跟上面的XML文件中的屬性相對應:

User.java:

package cn.zifangsky.xml;
 
import java.util.List;
 
public class User {
    private String name;
    private String sex;
    private int age;
    private String contact;
    private List<String> ownPet;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getContact() {
        return contact;
    }
 
    public void setContact(String contact) {
        this.contact = contact;
    }
 
    protected List<String> getOwnPet() {
        return ownPet;
    }
 
    protected void setOwnPet(List<String> ownPet) {
        this.ownPet = ownPet;
    }
 
    @Override
    public String toString() {
        return "User [name=" + name + ", sex=" + sex + ", age=" + age
                + ", contact=" + contact + ", ownPet=" + ownPet + "]";
    }
}

iii)測試代碼:

XMLtoJava.java:

package cn.zifangsky.xml;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class XMLtoJava {
 
    public User parseXMLtoJava(String xmlPath){
        User user = new User();
        List<String> ownPet = new ArrayList<String>();
         
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new File(xmlPath));
            Element rootElement = document.getRootElement();  //獲取根節(jié)點
             
            List<Element> children = rootElement.elements();  //獲取根節(jié)點的子節(jié)點
            //遍歷
            for(Element child : children){
                String elementName = child.getName();  //節(jié)點名
                String elementValue = child.getStringValue();  //節(jié)點值
                switch (elementName) {
                case "name":
                    user.setName(elementValue);
                    break;
                case "sex":
                    user.setSex(elementValue);
                    break; 
                case "age":
                    user.setAge(Integer.valueOf(elementValue));
                    break;
                case "contact":
                    user.setContact(elementValue);
                    break; 
                case "ownPet":
                    ownPet.add(elementValue);
                    break; 
                default:
                    break;
                }
     
            }
            user.setOwnPet(ownPet);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
     
    public static void main(String[] args) {
        XMLtoJava demo = new XMLtoJava();
        User user = demo.parseXMLtoJava("src/cn/zifangsky/xml/demo2.xml");
        System.out.println(user);
    }
 
}

經(jīng)過前面的分析之后,上面這個代碼也是很容易理解的:通過遍歷節(jié)點,如果節(jié)點名跟Java類中的某個屬性名相對應,那么就將節(jié)點值賦值給該屬性

iv)上面的代碼輸出如下:

User [name=zifangsky, sex=男, age=100, contact=https://www.zifangsky.cn, ownPet=[旺財, 九頭貓妖]]

(3)解析一個XML文件并盡可能原樣輸出:

DOM4JTest2:

package cn.zifangsky.xml;
 
import java.io.File;
import java.util.List;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class DOM4JTest2 {
 
    /**
     * 解析XML文件并盡可能原樣輸出
     * 
     * @param xmlPath
     *            待解析的XML文件路徑
     * @return null
     * */
    public void parse(String xmlPath) {
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new File(xmlPath));
            Element rootElement = document.getRootElement();
 
            print(rootElement, 0);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 打印一個XML節(jié)點的詳情
     * 
     * @param element
     *            一個XML節(jié)點
     * @param level
     *            用于判斷xml節(jié)點前縮進多少的標識,每深入一層則多輸出4個空格
     * @return null
     * */
    public void print(Element element, int level) {
        List<Element> elementList = element.elements(); // 當前節(jié)點的子節(jié)點List
 
        // 空格
        StringBuffer spacebBuffer = new StringBuffer("");
        for (int i = 0; i < level; i++)
            spacebBuffer.append("    ");
        String space = spacebBuffer.toString();
 
        // 輸出開始節(jié)點及其屬性值
        System.out.print(space + "<" + element.getName());
        List<Attribute> attributes = element.attributes();
        for (Attribute attribute : attributes)
            System.out.print(" " + attribute.getName() + "=\""
                    + attribute.getText() + "\"");
 
        // 有子節(jié)點
        if (elementList.size() > 0) {
            System.out.println(">");
            // 遍歷并遞歸
            for (Element child : elementList) {
                print(child, level + 1);
            }
            // 輸出結束節(jié)點
            System.out.println(space + "</" + element.getName() + ">");
 
        } else {
            // 如果節(jié)點沒有文本則簡化輸出
            if (element.getStringValue().trim().equals(""))
                System.out.println(" />");
            else
                System.out.println(">" + element.getStringValue() + "</"
                        + element.getName() + ">");
        }
 
    }
 
    public static void main(String[] args) {
        DOM4JTest2 test2 = new DOM4JTest2();
        test2.parse("src/cn/zifangsky/xml/demo3.xml");
 
    }
 
}

這段代碼同樣沒有什么新的東西,原理就是利用遞歸來不斷進行解析輸出,注意一下不同層次的節(jié)點的縮進即可。剛開始測試時建議用一些結構比較簡單的代碼,如上面的demo1.xml和demo2.xml文件。在測試沒問題時可以選擇一些復雜的XML文件來測試是否能夠正常輸出,比如:

demo3.xml:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://wadl.dev.java.net/2009/02"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <grammars />
    <resources base="http://localhost:9080/Demo/services/json/checkCode">
        <resource path="/">
            <resource path="addCheckCode">
                <method name="POST">
                    <request>
                        <representation mediaType="application/octet-stream" />
                    </request>
                    <response>
                        <representation mediaType="application/xml">
                            <param name="result" style="plain" type="xs:int" />
                        </representation>
                        <representation mediaType="application/json">
                            <param name="result" style="plain" type="xs:int" />
                        </representation>
                    </response>
                </method>
            </resource>
            <resource path="findCheckCodeByProfileId">
                <method name="POST">
                    <request>
                        <representation mediaType="application/octet-stream">
                            <param name="request" style="plain" type="xs:long" />
                        </representation>
                    </request>
                    <response>
                        <representation mediaType="application/xml" />
                        <representation mediaType="application/json" />
                    </response>
                </method>
            </resource>
        </resource>
    </resources>
</application>

為什么我在標題上說的是盡可能原樣輸出,其原因就是上面那段解析代碼在碰到下面這種XML節(jié)點時,輸出就不一樣了:

<dc:creator><![CDATA[admin]]></dc:creator>
<category><![CDATA[運維]]></category>
<category><![CDATA[zabbix]]></category>
<category><![CDATA[端口]]></category>

這段XML文檔節(jié)點最后輸出如下:

<creator>admin</creator>
<category>運維</category>
<category>zabbix</category>
<category>端口</category>


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276