2016-08-11 4 views
1

私はXMLを持っていて、xmlドキュメントから特定の要素を削除します。最初は、これらの要素に含まれる制御文字を削除するか、これらの要素のデータを完全に削除したいと考えました。xmlの特定の反復要素を削除します。

私のサンプルペイロードは以下の通りです。私は履歴書/コンテンツとコメントを削除したいと思っていました。

<Jobs> 
<candidates> 
    <address2/> 
     <application> 
      <comments>&lt;BR />Test emp1&#x0004;711 Newberg St. #4 Oregon, CA 97229 444-123-9752 [email protected]&#x0007;&#x0007;&lt;BR />authorName: Test 1 &#10; &#10;comment content: &lt;b>To: test test ([email protected])&lt;/b>&lt;br />&lt;br />From: abc Recruitment Team ([email protected])&lt;br />&lt;br />Subject: Tester at abc&lt;br />&lt;br />&lt;TEXTFORMAT LEADING="2">&lt;P ALIGN="LEFT">&lt;FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0">Hello test,&lt;/FONT>&lt;/P>&lt;/TEXTFORMAT>&lt;TEXTFORMAT LEADING="2">&lt;P ALIGN="LEFT">&lt;FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0">&lt;/FONT>&lt;/P>&lt;/TEXTFORMAT>&lt;TEXTFORMAT LEADING="2">&lt;P ALIGN="LEFT">&lt;FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0">Thank you for your interest in our abc opening. &lt;/FONT>&lt;/P></comments> 
     <disposition>tesr Skills match</disposition> 
     <eId>xyz</eId> 
     <gender>Male</gender> 
     <lastUpdatedDate>1340687163</lastUpdatedDate> 
     <race>Undefined</race> 
     <resume> 
      <content>Test&lt;BR />Human Resources Manager/Business Partner &lt;BR /> portland, Oregon; 4454; &lt;BR /> Phone 020 444456;Mobile 088768999;E-mail [email protected] &lt;BR /></content> 
      <format>Text</format> 
     </resume> 
     <sentDate>1789993473</sentDate> 
     <source>Linkedin</source> 
     <sourceType>Import</sourceType> 
     <veteranStatus>Undefined</veteranStatus> 
     <workflowState>Offer Accepted</workflowState> 
     <city>portland</city> 
     <companyName/> 
     <country>US</country> 
     <eId>xyz</eId> 
    </application> 
</candidates> 
<candidates> 
    <address2/> 
     <application> 
      <comments>&lt;BR />Test emp2&#x0004;711 Newberg St. #4 Oregon, CA 97229 444-123-9752 [email protected]&#x0007;&#x0007;&lt;BR />authorName: Test 2 &#10; &#10;</comments> 
     <disposition>Skills match</disposition> 
     <eId>xyz</eId> 
     <gender>female</gender> 
     <lastUpdatedDate>1340687163</lastUpdatedDate> 
     <race>Undefined</race> 
      <resume> 
       <content>Test&lt;BR />Program Manager/Business Partner  &lt;BR /> portland, Oregon; 4454; &lt;BR /> Phone 020 444456;Mobile 088768999;E-mail [email protected] &lt;BR /></content> 
      <format>Text</format> 
     </resume> 
     <sentDate>178444473</sentDate> 
     <source>Linkedin</source> 
     <sourceType>Import</sourceType> 
     <veteranStatus>Undefined</veteranStatus> 
     <workflowState>Offer Accepted</workflowState> 
     <city>portland</city> 
     <companyName/> 
     <country>US</country> 
     <eId>xyzabc</eId>`enter code here` 
     </application> 
    </candidates> 
    </Jobs> 
+1

あなたの試みは何ですか? – SomeDude

答えて

0

DocumentBuilderFactoryを使用してDocumentにXMLを構築し、以下のような関数に渡し:(nodeNameは「コメント」などになります)

 removeAllNodes(Document doc, String nodeName){ 
     DocumentTraversal t = (DocumentTraversal) doc; 
     Node root = doc.getDocumentElement(); 
     NodeIterator iterator = 
       t.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, null, true); 
     for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { 
      Element e = (Element) n; 
      if (nodeName.equals(e.getTagName())) 
       root.removeChild(e); 
     } 
    } 
関連する問題