简体   繁体   English

无法使用Java将字符串xml转换为Node并进行迭代

[英]Not able to Convert string xml to Node using java and iterate

I'm trying to convert xml string data to NodeList and iterate.. But, I'm not able to. 我正在尝试将xml字符串数据转换为NodeList并进行迭代。。但是,我无法。 I wanted to iterate the nodelist and fetch the xml data inside it. 我想迭代节点列表并获取其中的xml数据。 Here is my code 这是我的代码

    String arrayOfErrorContext = "<item><errorCode>1</errorCode><errorDescription></errorDescription></item>" +
            "<item><errorCode>1</errorCode><errorDescription></errorDescription></item>";
    if(arrayOfErrorContext!= null && !arrayOfErrorContext.isEmpty()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try
        {
            builder = factory.newDocumentBuilder();
            Document document = builder.parse( new InputSource( new StringReader( arrayOfErrorContext) ) );
            NodeList nList1 = document.getElementsByTagName("item");
            //iteration Logic on nList1
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Anything wrong in the above code? 上面的代码有什么问题吗?

first of all correct the XML string. 首先更正XML字符串。 pu a root element like .. pu ..这样的根元素

you are doing it in wrong way.. You need to convert string into dom.Document. 您正在以错误的方式进行操作。您需要将字符串转换为dom.Document。 and then try to get NodeList from it. 然后尝试从中获取NodeList。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder;  
        try  
        {  
            builder = factory.newDocumentBuilder();  
            Document document = builder.parse( new InputSource( new StringReader( arrayOfErrorContext ) ) );  
            NodeList nList1 = document.getElementsByTagName("item");
            //iteration Logic on nList1
        } catch (Exception e) {  
            e.printStackTrace();  
        } 

in this iteration logic get a node 在这个迭代逻辑中得到一个节点

 nList1.item(0).getFirstChild()

and iterate by fetching next node using. 并通过使用获取下一个节点进行迭代。

 .getNextSibling()

example iteration Logic: 示例迭代逻辑:

public static void recurseNode(Node node) {
        System.out.println(node.getNodeName() + node.getTextContent());

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentNode = nodeList.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                // calls this method for all the children which is Element
                recurseNode(currentNode);
            }
        }

complete code: 完整的代码:

import java.io.StringReader;

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

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class EnterExitListener {

    static Node e = null;

    public static void main(String[] argd) {
        String arrayOfErrorContext = "<root><item><errorCode>1</errorCode><errorDescription></errorDescription></item><item><errorCode>1</errorCode><errorDescription></errorDescription></item></root>";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(
                    arrayOfErrorContext)));
            NodeList nList1 = document.getElementsByTagName("root");
            int i = nList1.getLength();

            e = nList1.item(0).getFirstChild();
            e.getChildNodes();
            recurseNode(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void recurseNode(Node node) {
        System.out.println(node.getNodeName() + node.getTextContent());

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentNode = nodeList.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                // calls this method for all the children which is Element
                recurseNode(currentNode);
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM