Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:19:10+00:00 2026-05-31T12:19:10+00:00

I have the following simplified XML: <?xml version="1.0" encoding="UTF-8"?> <ExportData> <Rows> <R> <companyCodestringtrue>101</companyCodestringtrue> <transactionQualifierstring>Sales</transactionQualifierstring>

  • 0

I have the following simplified 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>

I need to delete each complete <R> element if the <productQuantityinttrue> element equals "0".

I came up with the following Java code:

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) {
    }
}

}

I am not sure if my method "traversingXML" is working properly. My problem right now is that the adapted XML structure (one deleted) is not written to newdoc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:19:11+00:00Added an answer on May 31, 2026 at 12:19 pm

    You don’t copy the original document to newdoc; instead you create a new, empty XML document.

    Instead, try this code:

     ...
     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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8"?> <ExportData> <TransportHeader> <Timestamp>2011-01-16 06:00:33</Timestamp>
I have the following simplified XML: <?xml version="1.0" encoding="UTF-8" ?> <MATMAS05> <IDOC BEGIN="1"> <E1MARAM
I have the following simplified XML source: <?xml version=1.0 encoding=UTF-8?> <MVKE> <item> <MANDT>025</MANDT> <MATNR>000000000000000551</MATNR>
I have the following (simplified XML): <?xml version=1.0 encoding=UTF-8 ?> <products> <product> <artnr>xxx1</artnr> </product>
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
So I have an xml file with the following simplified xml file contents: <CollectionItems>
I have the following (highly simplified) XML document that I am reading into my
I have the following XML (it is simplified and most attributes are omitted): <Document>
I have a document which when simplified looks like this: <?xml version=1.0?> <document> <br/>
I have the following XML (simplified): <node1> <node2> <node3> </node3> </node2> </node1> And I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.