2012-03-16 17 views
0

を提出するようになってXMLを記述することはできません。JavaのDOMは:私は、以下の単純化されたXMLを持って

<?xml version="1.0" encoding="UTF-8"?> 
<ExportData> 
<Rows> 
    <R> 
     <companyCodestringtrue>101</companyCodestringtrue> 
     <transactionQualifierstring>Sales</transactionQualifierstring> 
     <menuItemNumberlong>4302150</menuItemNumberlong> 
     <productQuantityinttrue>14</productQuantityinttrue> 
     <productValueInclVATdecimaltrue>1.90</productValueInclVATdecimaltrue> 
     <productValueExclVATdecimaltrue>1.775701</productValueExclVATdecimaltrue> 
    </R> 
    <R> 
     <companyCodestringtrue>101</companyCodestringtrue> 
     <transactionQualifierstring>Sales</transactionQualifierstring> 
     <menuItemNumberlong>333555</menuItemNumberlong> 
     <productQuantityinttrue>0</productQuantityinttrue> 
     <productValueInclVATdecimaltrue>3.90</productValueInclVATdecimaltrue> 
     <productValueExclVATdecimaltrue>3.775701</productValueExclVATdecimaltrue> 
    </R> 
    <R> 
     <companyCodestringtrue>101</companyCodestringtrue> 
     <transactionQualifierstring>Sales</transactionQualifierstring> 
     <menuItemNumberlong>1235665</menuItemNumberlong> 
     <productQuantityinttrue>5</productQuantityinttrue> 
     <productValueInclVATdecimaltrue>4.90</productValueInclVATdecimaltrue> 
     <productValueExclVATdecimaltrue>4.775701</productValueExclVATdecimaltrue> 
    </R> 
</Rows> 
</ExportData> 

<productQuantityinttrue>要素が「0」に等しい場合、私は完全な各<R>要素を削除する必要があります。

私は、次のJavaコードを思い付いた:

package filterPositions; 

import java.io.File; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Result; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerConfigurationException; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

public class FilterPositions { 

public static String result = ""; 

public static void main(String[] args) throws Exception { 
    try { 

     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     File filePath = new File("C:/LSA_SALES_EXPORT_1507_test_zero_qu.xml"); 
     Document doc = docBuilder.parse(filePath); 

     Node rootNode = doc.getDocumentElement(); 
     final Element element = doc.getDocumentElement(); 

     // output new XML Document 
     DocumentBuilder parser = docFactory.newDocumentBuilder(); 
     Document newdoc = parser.newDocument(); 
     newdoc.adoptNode(traversingXML(element)); 

     writeXmlFile(newdoc, "LSA_SALES_EXPORT_1507_test_zero_qu_OUT.xml"); 
     System.out.println("Done..."); 
     System.out.println("Exiting..."); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static Element traversingXML(Element element) { 
    NodeList positionen = element.getElementsByTagName("R"); 
    Element e = null; 
    for (int i = 0; i < positionen.getLength(); i++) { 
     e = (Element) positionen.item(i); 
     for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) { 
      if (child instanceof Element && "productQuantityinttrue".equals(child.getNodeName())&& "0".equals(child.getTextContent())) { 
       e.getParentNode().removeChild(e); 
      } 
     } 
    } 
    System.out.println(e); 
    return e; 
} 

public static void writeXmlFile(Document doc, String filename) { 
    try { 
     // Prepare the DOM document for writing 
     Source source = new DOMSource(); 

     // Prepare the output file 
     File file = new File(filename); 
     Result result = new StreamResult(file); 

     // Write the DOM document to the file 
     Transformer xformer = TransformerFactory.newInstance() 
       .newTransformer(); 
     xformer.transform(source, result); 
    } catch (TransformerConfigurationException e) { 
    } catch (TransformerException e) { 
    } 
} 

} 

私は私の方法「traversingXMLは」正常に動作しているかどうかわからないです。私の問題は、適合したXML構造(削除済み)がnewdocに書き込まれないということです。

私は間違っていますか?ご協力ありがとうございました。

敬具、 ピーター

答えて

1

あなたはnewdocに原稿をコピーしないでください。代わりに、新しい空のXML文書を作成します。

代わりに、このコードを試してみてください。

... 
final Element element = doc.getDocumentElement(); // original code up to here 

traversingXML(element); // delete the node 

writeXmlFile(doc, "LSA_SALES_EXPORT_1507_test_zero_qu_OUT.xml"); // save modified document 
+0

こんにちはアーロン、それが機能していません。たとえ私が// traversingXML(要素)をしても。ターゲットXMLは空です。私はそれがターゲットにソースを書き込むと思った。他のアイデアはありますか? Peter PS:Eclipseのデバッグでは、ソースXMLは決して読まれないようですね! – Peter

+0

私は "writeXMLFile"メソッドで間違いを見つけました: 'Source source = new DOMSource(doc);' – Peter

+0

'doc'が空であれば、それ以外は失敗します。 'docBuilder.parse(filePath);'が失敗すると、例外がスローされます。ソースXMLが読めないことをどのように知っていますか?デバッガで 'doc'を見ましたか?どの値に 'rootElement'フィールドがありますか? –

関連する問題