2011-07-24 16 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> 

私は子ノードを読み込むしようとしていながら、それは第二の文字列のためにELEMENT_NODEのための第一の文字列とnullをTEXT_NODEにnull子を返して、これは間違っている、それは問題をAPIである??私は次のコードを使用してい

...それは文字列

   <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> 
として、私は::の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を使用していますコンパイル

パースしようとすると

  <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でコンパイルされます。

答えて

0

おそらくあなたはNode.cloneNode()方法たい:標準出力へdomを取得したり、あなたを提出するには

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

をでき書き込み:

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と見なされます。

Documentをインポートしているので(JavaDocごとに)、あなたはimportNode()の呼び出しで例外が発生しないことに驚いています。

質問:特定のノードタイプのみを接続する場合は、ノードのタイプを使用してテストする必要があります。 (:これはコンパイルされていない、構文エラーが含まれている場合があります):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:OK、その場合、私はあなたが何をしようとしているのか分かりません。私は* complete *、* compilable *の例を含むようにあなたの投稿を編集することをお勧めします。次に、入力、実際の出力、および期待される出力を表示します。しかし、私があなたに保証できることの1つは:*それは** APIに問題がない**ことです。 – parsifal

+0

編集しました。さらに詳しい説明が必要な場合はお知らせください。上記のコードがコンパイルされることにご注意ください。私は現在、このコードを使って作業しています。変数を変更しました。 – Dhrubo

関連する問題