有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

混合文本和元素节点时XML子节点迭代的java问题

我尝试解析以下字符串以形成一个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_节点返回null child,第二个字符串的ELEMENT_节点返回null,这是错误的,是API问题吗

我正在使用以下代码。。。它是编译的,我用的是Java6

        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);

输入::作为字符串

             <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.... 

然后输出为“ThisWangChu”

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.

希望我清楚。以上代码用Java6编译


共 (2) 个答案

  1. # 1 楼答案

    可能您需要^{}方法:

    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放入标准输出或文件,您可以编写:

    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>
    
  2. # 2 楼答案

    我假设这是Java

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

    现在来看您提出的问题:如果只想附加特定的节点类型,则需要使用节点的类型进行测试。switch语句是最简单的(注意:尚未编译,可能包含语法错误):

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