当混合使用文本和元素节点时,XML子节点迭代问题

时间:2011-07-24 13:39:09

标签: java xml

我试图解析以下字符串以形成xml文档,然后尝试提取所有子节点并添加到我已经可用的其他文档对象。

<dhruba><test>this</test>that<test2>wang chu</test2> something.... </dhruba>

<dhruba>this is text node <test>this</test>that<test2>wang chu</test2> anything..</dhruba>

当我尝试读取子节点时,它为第一个字符串返回TEXT_NODE的null子元素,为第二个字符串返回ELEMENT_NODE的null,这是错误的,是API问题吗?

我正在使用以下代码...它编译,我正在使用java 6.

        Node n = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                try {
                    db = dbf.newDocumentBuilder();
                } catch (ParserConfigurationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                dom = db.newDocument();
                Element rootEle = dom.createElement("resources");
        // adding the root element to the document
        dom.appendChild(rootEle);

        Element element = dom.createElement("string");

        element.setAttribute("name", "some_name");
        try {

            n = db.parse(new InputSource(new StringReader("<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>"))).getDocumentElement();
            n = dom.importNode(n, true);


            NodeList nodeList = n.getChildNodes();
            int length = nodeList.getLength();
            System.out.println("Total no of childs : "+length);
            for(int count = 0 ; count < length ; count++ ){
                Node node = nodeList.item(count);
                if(node != null ){
                    element.appendChild(node);
                }
            }
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        rootEle.appendChild(element);

INPUT :: as string

             <dhruba><string name="some_name">
                        that
                        <test>this</test>                             
                        <test2>node   value</test2>
                        some text
                     </string>
              </dhruba>

预期输出::作为文件

               <string>
                 <string name="some_name">
                            <test>this</test>
                             <test2>node   value</test2>
                 </string>
              </string>

如果我尝试解析

          <test>this</test>that<test2>wang chu</test2> something.... 

然后输出为“thiswang chu”

Why is this happening?  what needs to be done if I want to add following node under another document element, i.e. <string>.
    <test>this</test>
                        that                             
                        <test2>node   value</test2>
                        some text 
[notice that it does not have <dhruba>] inside parent node of another 
document.
希望我很清楚。上面的代码在Java 6中编译

2 个答案:

答案 0 :(得分:1)

我会假设这是Java。

首先,我很惊讶您的importNode()调用没有异常,因为您正在导入Document,这不应该被允许(根据JavaDoc)。

现在问你问:如果你只想附加特定的节点类型,你需要使用节点的类型进行测试。 switch语句是最简单的(注意:这还没有编译,可能包含语法错误):

switch (n.getNodeType())
{
    case ELEMENT_NODE :
        // append the node to the other tree
        break;
    default :
        // do nothing
}

答案 1 :(得分:0)

您可能需要Node.cloneNode()方法:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document dom = db.newDocument();

Element element = dom.createElement("string");
element.setAttribute("name", "some_name");

String inputXMLString = 
    "<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>";
Node n = db.parse(new InputSource(new StringReader(inputXMLString))).getDocumentElement();
n = dom.importNode(n, true);

NodeList nodeList = n.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i)
{
    Node node = nodeList.item(i);
    element.appendChild(node.cloneNode(true));
}
dom.appendChild(element);

要将dom导入stdout或文件,您可以写:

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result); 

结果:

<string name="some_name">
<test>this</test>that<test2>node value</test2> some text</string>