2011-07-24 37 views
1

我试图解析下列字符串以形成一个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> 

,而我想读的子节点,则返回null孩子TEXT_NODE为1弦和空值ELEMENT_NODE的第二根弦,这是错误的,是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 ::作为字符串

   <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楚”

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

回答

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到标准输出或文件,你可以写:

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> 
+0

非常感谢Grzegorz,cloneNode(true)工作正常。你为我节省了更多的时间。 – Dhrubo

+0

@Dhrubo:不客气:)您可能会将我的答案标记为已接受(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –

+0

如何接受?我在这个网站是新的..请指导。我已经使用上面的链接,但它是一个meta.stackoverflow网站,我很困惑点击什么.. :( – Dhrubo

1

我会假设这是Java。

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

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

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

不,importNode正在返回节点,如果我添加父节点,即而不是试图添加它的子节点,它会添加罚款并生成良好的输出,但与父元素,我不想要。另外,我需要所有类型的节点,TEXT_NODE或ELEMENT_NODE,所以我没有使用检查,并且令人惊讶的是,根据给定的输入,它对任何一个节点类型都返回null。 – Dhrubo

+0

@dhrubo:好的,在那种情况下,我不知道你在做什么。我建议你编辑你的文章以包含*完整的*,*可编辑的*例子。然后显示输入,实际输出和预期输出。但是,我可以向你保证的一件事是:*它不是** API的问题。 – parsifal

+0

已修改,请让我知道是否需要进一步说明,并注意上面的代码编译。我目前正在使用这个代码,我刚刚改变了我的变量。 – Dhrubo