how to pass a parsed doccument? - java

Hi all and thanks for your time and help.
Im trying to parse a xml file in the "res/raw" folder and passing the result to other class to work with it.
I'm stuck and cannot find any solution, Im trying to learn programming, so, the whole code may be wrong and I failed catastrophically from the beginning.
this is the parser, it get a document from the raw folder and return the parsed document.
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import android.app.Activity;
public class Pars extends Activity{
public Document doc(){
InputStream is = getResources().openRawResource(R.raw.talechap01);
Document docout = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
docout = builder.parse(is);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docout;
}
}
this class get the parsed document and find some content using xpath and pass it in a return.
I can not pass the parsed document "docout" from the class "Pars"
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Commanders {
public static String getStory(String spage) {
XPathExpression expr;
XPath xpath = XPathFactory.newInstance().newXPath();
Object result = null;
String num = spage;
String par = null;
Document doc = //Here I need the document parsed from the "Pars" class
try {
expr = xpath.compile("//decision"+num+"/p//text()");
result = expr.evaluate(doc, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
e.printStackTrace();
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
par = nodes.item(i).getNodeValue();
// System.out.println(par);
}
return par;
}
}
Here is an example from the xml.
I'm trying to get the content of "" depending of the "decisionXXX" number.
<talechap01>
<Start></Start>
<decision000 id="000">
<p> part 1 text</p>
<p> second row text</p>
<pick chs= "001" name= "go to decision001"/>
<pick chs= "002" name= "go to decision002"/>
</decision000>
<decision001 id="001">
<p>parte 2</p>
<pick chs = "002" name= "go to decision002"/>
<pick chs = "003" name= "go to decision003"/>
</decision001>
I'm doing something/all wrong?
Is there even possible to do?

Maybe I don't understand something, but it seems like attribute "id" is a decision number,
i mean next
decision000 id=000
If it's true then you can first find nodes which start with and then select by id
I can't check it now, but it should work, try next expression:
//*[starts-with(local-name(), 'decision')][#id='your_num_value']

You need to adjust the function signature of Commanders#getStory(java.lang.String) so it takes your extra doc argument. You can then execute your code like...
Pars pars = new Pars();
Document doc = pars.doc();
Commanders.getStory(doc, spage)

Related

Special characteres error on saving XML file in Java ("<" and ">")

I am trying to edit a specific key inside an XML file. The problem is that after the file is saved, the '<' and '>' characters are transformed in coded characters (respectively: "& lt;" and "& gt;").
XML File:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<system.web>
<httpRuntime targetFramework="X.X"/>
</system.web>
<connectionStrings>
<add connectionString="Data Source=XXXX;user Id=YYYYY;password=ZZZZZ;" name="Entities" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
I'm updating the value of the entire connectionStrings key:
package br.com.sedna.bitbucket.plugin.enviromentsetup.utils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ModifyXmlFile {
private File _inputFile;
private String _filePath;
private DocumentBuilderFactory _docFactory;
private DocumentBuilder _docBuilder;
private Document _doc;
private Node _xmlObject;
private String CONNECTION_STRINGS = "<add connectionString=\"Data Source=XXXX;user Id=%s;password=ZZZZZ;\" name=\"Entities\" providerName=\"Oracle.ManagedDataAccess.Client\"/>";
public ModifyXmlFile(String xmlPath){
try {
this._filePath = xmlPath;
this._inputFile = new File(this._filePath);
this._docFactory = DocumentBuilderFactory.newInstance();
this._docBuilder = _docFactory.newDocumentBuilder();
this._doc = _docBuilder.parse(_inputFile);
this._xmlObject = _doc.getFirstChild();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ModifyXmlFile setNodeValue(String nodeName, String nodeValue){
Node nodeNameObject = this._doc.getElementsByTagName(nodeName).item(0);
try {
// updating the value of connectionStrings key
nodeNameObject.setTextContent(String.format(CONNECTION_STRINGS, nodeValue));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
DOMSource source = new DOMSource(this._doc);
StreamResult consoleResult = new StreamResult(new File(this._filePath));
transformer.transform(source, consoleResult);
System.out.println("Done");
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
}
Running this class:
ModifyXmlFile xmlHelper = new ModifyXmlFile("C:\\Web.config");
xmlHelper.setNodeValue("connectionStrings", "NEWVALUE");
The result:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<system.web>
<httpRuntime targetFramework="X.X"/>
</system.web>
<connectionStrings><add connectionString="Data Source=XXXX;user Id=NEWVALUE;password=ZZZZZ;" name="Entities" providerName="Oracle.ManagedDataAccess.Client"/></connectionStrings>
</connectionStrings>
</configuration>
I have tried changing the encode type without success.
I have modified the setNodeValue method. The relevant part follows:
public ModifyXmlFile setElementAttributeValue(String nodeXPath, String attributeName, String newAttributeValue) throws XPathExpressionException{
try {
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node)xPath.evaluate(nodeXPath, _doc, XPathConstants.NODE);
node.getAttributes().getNamedItem(attributeName).setNodeValue(String.format(CONNECTION_STRINGS, newAttributeValue));
Calling this via
setElementAttributeValue("/configuration/connectionStrings/add", "connectionString", "NEWVALUE" );
changes the value of the attribute connectionString on the element add.
There are several problems in the original code:
the consoleResult is named confusingly - the StreamResult points to a file
the parameter nodeName actually stands for parent node of the element to be edited and this is indeed what is retrieved per this._doc.getElementsByTagName(nodeName).item(0) - at least when setNodeValue is called with "connectionStrings" for nodeName.
the name of the attribute to be edited is hard-wired into the code.
as T.J. Crowder mentioned, the setTextContent method the original code is calling is used to set text content, i.e. the text between >< of some element. It is neccessary to distinguish between elements, attributes and text content and be aware of the fact that these are not interchangeable. It is not possible to add new element by adding text content which looks like (is formatted like) an element.
Please note that the new code is not production ready as at the very least the handling of potential null values would need to be added, e.g. on the result of getNamedItem(attributeName).

Can't make Java created XML work with XSL

I'm creating an XML using DOM, I open the XML with explorer and it seems fine.
After that I created an XSL for it, add the reference to it programatically to the XML and boom, it works no longer.
I inspect the code in the browser and save the file as XML.
I open the XML with notepad++ and noticed the npp shows the XML file is encoded in "UCS-2 LE BOM" even though the XML header sais it's in "UTF-8", I don't know if this has to do with being saved from the browser or it's because of Java transformer.File encoding according to npp
Anyway, I change the file encoding in npp to "UTF-8", open it again and it works flawlessly.
I've tried changing the transformer's encoding property transformer.setOutputProperty("encoding", "ISO-8859-1") to different values but it will just change the XML header.
I googled around but I haven't found anything that's worked so far.
This is the code where the XML is created
package modelo;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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;
public class DAOXML {
private static final String rutaUno = ".\\VerLibro.xml";
private static final String rutaListado = ".\\listado.xml";
public static boolean toXML(ArrayList<Libro> libros2) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final String rutaActual = libros2.size()>1?rutaListado:rutaUno;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
doc.setDocumentURI(rutaActual);
doc.setXmlStandalone(true);
Element libros = doc.createElement("libros");
for(Libro l : libros2) {
Element libro = doc.createElement("libro");
Element isbn = doc.createElement("isbn");
isbn.setTextContent(l.getISBN());
libro.appendChild(isbn);
Element editorial = doc.createElement("editorial_apellidos");
editorial.setTextContent(l.getEditorial_apellidos());
libro.appendChild(editorial);
Element autor = doc.createElement("autor");
autor.setTextContent(l.getAutor());
libro.appendChild(autor);
Element categoria = doc.createElement("categoria");
categoria.setTextContent(l.getCategoria());
libro.appendChild(categoria);
Element titulo = doc.createElement("titulo");
titulo.setTextContent(l.getTitulo());
libro.appendChild(titulo);
Element ubicacion = doc.createElement("ubicacion");
ubicacion.setTextContent(l.getUbiacion());
libro.appendChild(ubicacion);
libros.appendChild(libro);
}
doc.appendChild(libros);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// transformer.setOutputProperty("encoding", "ISO-8859-1");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(rutaActual));
//result.setWriter(new PrintWriter(new File(rutaActual), "UTF-8"));
transformer.transform(source, result);
if(rutaActual == rutaUno) {
Node pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"VerLibro.xsl\"");
doc.insertBefore(pi, libros);
}
else {
Node pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"listado.xsl\"");
doc.insertBefore(pi, libros);
}
Desktop.getDesktop().open(new File(rutaActual));
return true;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
Thanks in advance.
Try adding this line before you call transform():
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

XPath works standalone, but does not work when put in a web app

I am looking to parse the XML, With the help of xpath expression I want to bring in all the node values whose node name is AuditRecord. Surprisingly I can do this in a standalone application but same code fails in the application. I didnt even change the XML. I am using the same XML file in the standalone application and web application I can see results in standlone application but I get 0 nodelist size when I try the same thing in webapplication.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestXmlNode {
private static final String TEST_XML = "C:/test.xml";
public static void main(String[] args) throws ParseException {
try {
//XPath xPath = XPathFactory.newInstance().newXPath();
/*System.out.println("*************************");
String expression = "//AuditRecord/DateTimeStamp";;
System.out.println("*************************");
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
System.out.println("Nodelist size"+nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
String date = nodeList.item(i).getFirstChild().getNodeValue();
System.out.println("Date after split "+date.split("T")[0]);
Date thedate = convertStringToDate(date);
Date todate = convertStringToDate("2014-01-01");
System.out.println("XML todate"+todate.toString());
if(thedate.after(todate))
System.out.println("-------------------------->"+ thedate.toString());
}*/
FileInputStream file = new FileInputStream(new File(TEST_XML));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
org.w3c.dom.Document xmlDocument = builder.parse(file);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression xPathExpr = xpath.compile("//AuditRecord/DateTimeStamp/text()");
Object result = xPathExpr.evaluate(xmlDocument, XPathConstants.NODESET);
print(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
public static void print(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(i+" "+nodes.item(i).getNodeValue());
}
}
}
Your web app probably doesn't have access to C:/test.xml as web apps generally aren't allowed to access the file system directly.
The presence of xmlparserv-2.0.jar in the classpath is issue, which does not allow to apply xpath expression. Any idea on how to move on

Java DOM parse html in xml node

i have a parser here:
package lt.prasom.functions;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.annotation.TargetApi;
import android.media.MediaRecorder.OutputFormat;
import android.util.Log;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
#TargetApi(8)
public final String getElementValue( Node elem , boolean html) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
//return child.getNodeValue();
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0), false);
}
}
And there's my sample xml :
<items>
<item>
<name>test</name>
<description>yes <b>no</b></description>
</item>
</items>
When i parse description i'm getting everything to tag ("yes"). So i want to parse raw data in description tag. I tried CDATA tag didin't worked. Is it any way without encoding xml?
Thanks!
I agree with the comments about this question not being complete, or specific enough for a direct answer (like modifying your source to work etc.), but I had to do something somewhat similar (I think) and can add this. It might help.
So if, IF, the content of the "description" element were valid XML all by itself, so say the document actually looked like:
<items>
<item>
<name>test</name>
<description><span>yes <b>no</b></span></description>
</item>
</items>
then you could hack out the content of the "description" element as a new XML Document and then get the XML text form that which would look then like:
<span>yes <b>no</b></span>
So a method something like:
/**
* Get the Description as a new XML document
*
*/
public Document retrieveDescriptionAsDocument(Document sourceDocument) {
Document document;
Node tmpNode;
Document document2 = null;
try {
// get the description node, I am just using XPath here as it is easy
// to read, you already have a reference to the node so just continue as you
// were doing for that, bottom line is to get a reference to the node
tmpNode = org.apache.xpath.XPathAPI.selectSingleNode(sourceDocument,"/items/item/description");
if (tmpNode != null) {
// create a new empty document
document2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// associate the node with the original document
sourceDocument.importNode(tmpNode, true);
// create a document fragment from the original document
DocumentFragment df = sourceDocument.createDocumentFragment();
// append the node you found, to the fragment
df.appendChild(tmpNode);
// create the Node to append to the new DOM
Node importNode = document2.importNode(df,true);
// append the fragment (as a node) to the new empty document
Document2.appendChild(importNode);
}
else {
// LOG WARNING
yourLoggerOrWhatever.warn("retrieveContainedDocument: No data found for XPath:" + xpathP);
}
} catch (Exception e) {
// LOG ERROR
yourLoggerOrWhatever.error("Exception caught getting contained document:",e);
}
// return the new doc, and the caller can then output that new document, that will now just contain "<span>yes <b>no</b></span>" as text, apply an XSL or whatever
return document2;
}

Java DOM: cannot write adapted XML to file

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.
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

Categories

Resources