How to avoid extra blank lines in XML generation with Java? - java

Currently I'm trying to develop some code with Java 9 and javax.xml libraries (both mandatory for my task) that edits an XML file and I'm having some weird issues adding child nodes.
This is the XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
</users>
and I want to edit it build something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user>
<name>A name</name>
<last-name>Last Name</last-name>
<username>username</username>
</user>
</users>
Now, the first run of the code adds a single blank line before the <user> node. When it runs for a second time fills with more blank lines:
<users>
<user>
<name>name</name>
<last-name>lastname</last-name>
<username>username</username>
</user>
<user>
<name>name</name>
<last-name>lastname</last-name>
<username>username</username>
</user>
</users>
This is the XML generated after running the program 2 times. As you can see, it adds blank lines before the <user> nodes and between the other nodes, exactly n-1 blank lines between nodes being n the times the code was executed.
Wondering what is the content of those nodes before updating the file I wrote the next code:
int i=0;
while (root.getChildNodes().item(i)!=null){
Node aux = root.getChildNodes().item(i);
System.out.println("Node text content: ".concat(aux.getTextContent()));
i++;
}
1st execution:
Node text content:
Node text content: namelastnameusername
2nd execution:
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content: namelastnameusername
3rd execution
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content: namelastnameusername
Finally, this is the Java code:
private static void saveUser(String firstName, String lastName, String username){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(databaseFile));
Element root = doc.getDocumentElement();
root.normalize();
// build user node
Element userNode = doc.createElement("user");
Element nameNode = doc.createElement("name");
Element lastNameNode = doc.createElement("last-name");
Element usernameNode = doc.createElement("username");
//build structure
nameNode.appendChild(doc.createTextNode(firstName));
lastNameNode.appendChild(doc.createTextNode(lastName));
usernameNode.appendChild(doc.createTextNode(username));
userNode.appendChild(nameNode);
userNode.appendChild(lastNameNode);
userNode.appendChild(usernameNode);
root.appendChild(userNode);
//write the updated document to file or console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(databaseFile));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
The only solution I could find is to delete blank lines after XML generation, but I think it's not a proper solution and I would like to find some alternatives first.
Any suggestions on how to tackle this problem?

I suspect it's the Transformer that's adding the blank lines.
Instead of using the default transformer (transformerFactory.newTransformer()), try passing in an XSLT that has xsl:strip-space set (transformerFactory.newTransformer(new StreamSource(new File(PATH_TO_XSLT_FILE)));)...
XSLT File
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

In Short: Actually, in Java 9, you may only take the way of deleting the blank line after xml generated or after xml parsed from file, like :
private void clearBlankLine(Element element) {
NodeList childNodes = element.getChildNodes();
for (int index = 0; index < childNodes.getLength(); index++) {
Node item = childNodes.item(index);
if (item.getNodeType() != 1 && System.lineSeparator()
.equals(item.getNodeValue())) {
element.removeChild(item);
} else {
if (item instanceof Element) {
clearBlankLine((Element) item);
}
}
}
}
Then invoke this with root element.
Details:
In the flow of xml generation, there are three lifecycle for each element parse: startElement,parse,endElement. While the indent feature is implemented in the startElement scope. Also the indent will add a blank line in document.
The invoke stack is different in java 8 between java 9:
In Java 8: ToStream#startElement-> ToStream#indent(IfNecessary)
In Java 9: ToStream#startElement->ToStream#flushCharactersBuffer(IfNecessary)->ToStream#indent(IfNecessary)
While the flushCharactersBuffer also do indent when we open the indent feature like: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Also the condition to invoke method: flushCharactersBuffer and method: indent almost same.
That means in Java 9, this would add two new line for each need indented element, result to blank lines appeared.

I found this solution using an XPath to be much cleaner than any of the others here (h/t to Isaac for his answer over at https://stackoverflow.com/a/12670194/1339923). It doesn't require a separate (i.e., XSLT) file, and doesn't require you to add 14 lines of Java to iterate over every node in the Document. Only 6 lines of code.
In the case of #pablo-r-grande's original question... right before this comment (i.e., just before loading the Document into the DOMSource):
//write the updated document to file or console
...I would add these lines:
// Generate list of all empty Nodes, them remove them
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) { // note the position of the '++'
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}

your solution and below suggestion are both works fine for me, please try with this test case,
public static void main(String[] args) {
saveUser("test one", "test two", "test three");
}
private static void saveUser(String firstName, String lastName, String username){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("second.xml"));
Element root = doc.getDocumentElement();
root.normalize();
// build user node
Element userNode = doc.createElement("user");
Element nameNode = doc.createElement("name");
Element lastNameNode = doc.createElement("last-name");
Element usernameNode = doc.createElement("username");
userNode.appendChild(nameNode).setTextContent(firstName); //set the text content
userNode.appendChild(lastNameNode).setTextContent(lastName);
userNode.appendChild(usernameNode).setTextContent(username);
root.appendChild(userNode);
//write the updated document to file or console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("second.xml"));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}catch (Exception e) {
e.printStackTrace();
}
}
second.xml (before execution)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
</users>
second.xml (first execution)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
</users>
second.xml (second execution)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
</users>
second.xml (third execution)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
<user>
<name>test one</name>
<last-name>test two</last-name>
<username>test three</username>
</user>
</users>
importing classes,
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
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 javax.xml.parsers.DocumentBuilder; // missing import class
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Related

How to transform each xml node with Saxon XSLT?

How can I use Saxon xslt transformation library to convert an xml file that contains many nodes to a plain csv string? Means, I want Saxon to concatenate each employee entry as csv, and put them all together.
This is the saxon setup, but I don't know how I could not transform an input xml file with it:
//false = does not required a feature from a licensed version of Saxon.
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
compiler.compile(new StreamSource("transformation.xslt"));
Serializer serializer = processor.newSerializer();
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
//TODO
//String result = serializer...serializeNodeToString();
I want to transform the following xml:
<employees stage="test">
<employee>
<details>
<name>Joe</name>
<age>34</age>
</details>
<address>
<street>test</street>
<nr>12</nr>
</address>
</employee>
<employee>
<address>....</address>
<details>
<!-- note the changed order of elements! -->
<age>24</age>
<name>Sam</name>
</details>
</employee>
</employees>
The result string should contain the following (one big string with linebreak separated csv lines):
test,Joe,34,test,12\n
test,Sam,24,...\n
Xslt might be similar to:
<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="employee">
<xsl:value-of select="name"/>
<xsl:value-of select="age"/>
</xsl:template>
</xsl:transform>
The XSLT template can be modified as below. The elements can be selected according to the required sequence.
<xsl:template match="employee">
<xsl:value-of select="ancestor::employees/#stage, details/name, details/age, address/street, address/nr" separator=", " />
<xsl:text>
</xsl:text>
</xsl:template>
After replacing the ... with some dummy values in the <address> element the following output is generated using the above template.
test, Joe, 34, test, 12
test, Sam, 24, test1, 123
For transforming the XML (using XSLT) in Java, I use the following code snippet most of the time. This method returns the transformed XML as a String. The required library is saxon9he.jar. You may have to upgrade the library version for using with XSLT 3.0
public static String transformXML(String xml, InputStream xslFile) {
String outputXML = null;
try {
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslFile));
Source xmlStream = new StreamSource(new StringReader(xml));
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
transformer.transform(xmlStream, result);
outputXML = writer.getBuffer().toString();
} catch (TransformerConfigurationException tce) {
tce.printStackTrace();
} catch (TransformerException te) {
te.printStackTrace();
}
return outputXML;
}

Xml Output generation from excel sheet

I am trying to convert an excel file into xml file in java.
try {
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("dataroot");
doc.appendChild(root);
Element Details = doc.createElement("DATA");
root.appendChild(Details);
for(int i=0; i<list.size()-2; i +=3 ) {
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
Details.appendChild(name);
Element id = doc.createElement("Empid");
id.appendChild(doc.createTextNode(String.valueOf(list.get(i+1))));
Details.appendChild(id);
Element ad = doc.createElement("Add");
ad.appendChild(doc.createTextNode(String.valueOf(list.get(i+2))));
Details.appendChild(ad);
Element mo = doc.createElement("Mobile");
mo.appendChild(doc.createTextNode(String.valueOf(list.get(i+3))));
Details.appendChild(mo);
}
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// format the XML nicely
aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
aTransformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
This is my code for reading an ArrayList of excel data (with values and null) and convert it into xml.
my output is:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<dataroot generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile>12333333</Mobile>
</DATA>
<DATA>
<Name>12333333</Name>
<Empid>Azam</Empid>
<Add>FBL321</Add>
<Mobile>Dhaka</Mobile>
</DATA>
<DATA>
<Name>Dhaka</Name>
<Empid>67778888</Empid>
<Add>Rony</Add>
<Mobile>Chandpur</Mobile>
</DATA>
<DATA>
<Name>Chandpur</Name>
<Empid>099776655</Empid>
<Add>Azam</Add>
<Mobile>9988</Mobile>
</DATA>
</dataroot>
But my desired xml output is:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
<DATA>
<Name>AZAM</Name>
<Empid>FBL321</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
</dataroot>
How i Can do it?
To have "DATA" tags, you shoud move the lines below into your for statement:
Element Details = doc.createElement("DATA");
root.appendChild(Details);
To add some property on "dataroot", use the code below:
root.setAttribute("generated", "2015-04-26T10:52:27");
On Mobile element, try this:
Element mo = doc.createElement("Mobile");
Element prop = doc.createElement("Prop");
prop.setAttribute("ID", "FAMILY");
prop.setAttribute("ValStr", "Mrs. YYYYYYYY");
Element prop2 = doc.createElement("Prop");
prop2.setAttribute("ID", "Personal");
prop2.setAttribute("ValStr", "Mrs. ZZZZZZZZ");
mo.appendChild(prop);
mo.appendChild(prop2);
Try to read about DOM parser on this tutorial as well. How to create XML file in Java – (DOM Parser)

Unexpected attribute in generated XML

I'm generating this XML:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap xmlns="" id="0">
<loc>http://x.ae/sitemap-url/ae/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="1">
<loc>http://x.bh/sitemap-url/bh/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="2">
<loc>http://x.eg/sitemap-url/eg/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="3">
<loc>http://x.ma/sitemap-url/ma/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="4">
<loc>http://x.om/sitemap-url/om/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="5">
<loc>http://x.qa/sitemap-url/qa/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="6">
<loc>http://x.tn/sitemap-url/tn/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
</sitemapindex>
I try to delete this attribute: <sitemap xmlns=""> but it's no possible.
What I'm doing wrong?
This is my code to generate XML:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
//ROOT ELEMENT:
Element rootElement = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "sitemapindex");
doc.appendChild(rootElement);
SortedSet<Country> countries = locationDao.readAllCountriesSorted();
int i =0;
for(Country c : countries){
Element siteMap = doc.createElement("sitemap");
siteMap.setAttribute("id", String.valueOf(i));
//PUT AN ID
Element loc = doc.createElement("loc");
Element lastMod = doc.createElement("lastmod");
loc.appendChild(doc.createTextNode("http://"+c.getDomain()+"/sitemap-url/"+c.getCode().toLowerCase()+"/sitemap-index.xml"));
lastMod.appendChild(doc.createTextNode(DateUtils.getCurrentDateString()));
rootElement.appendChild(siteMap);
siteMap.appendChild(loc);
siteMap.appendChild(lastMod);
i++;
}
;//always appear.
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//IDENTEM EL RESULTAT
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
//Si ho volem mostrar en un file:
StreamResult result = new StreamResult(new File("sitemapTest.xml"));
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("Document ready....");
I try to delete with removeAttribute method or removeAttNs but seems not working..
Create your sitemap, loc, lastmod elements in the proper namespace ( you currently create it in the null namespace, which in this case is not the default namespace) :
Element siteMap = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "sitemap");
//...
Element loc = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "loc");
Element lastMod = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "lastmod");
It is a while since i did XML with DOM in Java but if i remember correctly you must set the namespace on all elements, so your dom knows which namespace to use. If it does not know this, it will create an empty XML-Namepsace (short: xmlns) which results in the empty attribute you are seeing.
It's not an attribute, it's a namespace declaration. The effect of the namespace declaration is to ensure that your sitemap elements are not in a namespace. It's needed because when you created the sitemap elements, you asked for them to be in no namespace. If you want them in the namespace "http://www.sitemaps.org/schemas/sitemap/0.9", you must create them in that namespace, which you do using createElementNS().

Modify XML file with xPath

I want to modify an existing XML file using xPath. If the node doesn't exist, it should be created (along with it's parents if neccessary). An example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<param0>true</param0>
<param1>1.0</param1>
</configuration>
And here are a couple of xPaths I want to insert/modify:
/configuration/param1/text() -> 4.0
/configuration/param2/text() -> "asdf"
/configuration/test/param3/text() -> true
The XML file should look like this afterwards:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<param0>true</param0>
<param1>4.0</param1>
<param2>asdf</param2>
<test>
<param3>true</param3>
</test>
</configuration>
I tried this:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.transform.Transformer;
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.Node;
import org.w3c.dom.NodeList;
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
Document doc = domFactory.newDocumentBuilder().parse(file.getAbsolutePath());
XPath xpath = XPathFactory.newInstance().newXPath();
String xPathStr = "/configuration/param1/text()";
Node node = ((NodeList) xpath.compile(xPathStr).evaluate(doc, XPathConstants.NODESET)).item(0);
System.out.printf("node value: %s\n", node.getNodeValue());
node.setNodeValue("4.0");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(file));
} catch (Exception e) {
e.printStackTrace();
}
The node is changed in the file after running this code. Exactly what I wanted. But if I use one of the below paths, node is null (and therefore a NullPointerException is thrown):
/configuration/param2/text()
/configuration/test/param3/text()
How can I change this code so that the node (and non existing parent nodes as well) are created?
EDIT: Ok, to clarify: I have a set of parameters that I want to save to XML. During development, this set can change (some parameters get added, some get moved, some get removed). So I basically want to have a function to write the current set of parameters to an already existing file. It should override the parameters that already exist in the file, add new parameters and leave old parameters in there.
The same for reading, I could just have the xPath or some other coordinates and get the value from the XML. If it doesn't exist, it returns the empty string.
I don't have any constraints on how to implement it, xPath, DOM, SAX, XSLT... It should just be easy to use once the functionality is written (like BeniBela's solution).
So if I have the following parameters to set:
/configuration/param1/text() -> 4.0
/configuration/param2/text() -> "asdf"
/configuration/test/param3/text() -> true
the result should be the starting XML + those parameters. If they already exist at that xPath, they get replaced, otherwise they get inserted at that point.
If you want a solution without dependencies, you can do it with just DOM and without XPath/XSLT.
Node.getChildNodes|getNodeName / NodeList.* can be used to find the nodes, and Document.createElement|createTextNode, Node.appendChild to create new ones.
Then you can write your own, simple "XPath" interpreter, that creates missing nodes in the path like that:
public static void update(Document doc, String path, String def){
String p[] = path.split("/");
//search nodes or create them if they do not exist
Node n = doc;
for (int i=0;i < p.length;i++){
NodeList kids = n.getChildNodes();
Node nfound = null;
for (int j=0;j<kids.getLength();j++)
if (kids.item(j).getNodeName().equals(p[i])) {
nfound = kids.item(j);
break;
}
if (nfound == null) {
nfound = doc.createElement(p[i]);
n.appendChild(nfound);
n.appendChild(doc.createTextNode("\n")); //add whitespace, so the result looks nicer. Not really needed
}
n = nfound;
}
NodeList kids = n.getChildNodes();
for (int i=0;i<kids.getLength();i++)
if (kids.item(i).getNodeType() == Node.TEXT_NODE) {
//text node exists
kids.item(i).setNodeValue(def); //override
return;
}
n.appendChild(doc.createTextNode(def));
}
Then, if you only want to update text() nodes, you can use it as:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
Document doc = domFactory.newDocumentBuilder().parse(file.getAbsolutePath());
update(doc, "configuration/param1", "4.0");
update(doc, "configuration/param2", "asdf");
update(doc, "configuration/test/param3", "true");
Here is a simple XSLT solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="param1/text()">4.0</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<param2>asdf</param2>
<test><param3>true</param3></test>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<configuration>
<param0>true</param0>
<param1>1.0</param1>
</configuration>
the wanted, correct result is produced:
<configuration>
<param0>true</param0>
<param1>4.0</param1>
<param2>asdf</param2>
<test><param3>true</param3></test>
</configuration>
Do Note:
An XSLT transformation never "updates in-place". It always creates a new result tree. Therefore, if one wants to modify the same file, typically the result of the transformation is saved under another name, then the original file is deleted and the result is renamed to have the original name.
I've created a small project for using XPATH to create/update XML: https://github.com/shenghai/xmodifier
the code to change your xml is like:
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlfile);
XModifier modifier = new XModifier(document);
modifier.addModify("/configuration/param1", "asdf");
modifier.addModify("/configuration/param2", "asdf");
modifier.addModify("/configuration/test/param3", "true");
modifier.modify();
I would point you to a new/novel way of doing what you described, by using VTD-XML... there are numerous reasons why VTD-XML is far better than all other solutions provided for this question... here are a few links ...
Simplify XML processing with vtd-xml
Manipulate XML the Ximple Way
Processing XML with Java – A Performance Benchmark
dfs
import com.ximpleware.*;
import java.io.*;
public class modifyXML {
public static void main(String[] s) throws VTDException, IOException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("input.xml", false))
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/configuration/param1/text()");
XMLModifier xm = new XMLModifier(vn);
// using XPath
int i=ap.evalXPath();
if(i!=-1){
xm.updateToken(i, "4.0");
}
String s1 ="<param2>asdf</param2>/n<test>/n<param3>true</param3>/n</test>";
xm.insertAfterElement(s1);
xm.output("output.xml");
}
}

Selecting xml raw text

Given xml like this:
<container>
<item>
<xmlText>
<someTag>
<otherTag>
Text
</otherTag>
</someTag>
</xmlText>
</item>
<container>
I would like to select all text that is under item/xmlText. I would like to print all the content of this node with tags (someTag, otherTag).
I would prefer to handle with this with XPath, but this is part of Java program, so if there is such mechanism I could take it as well.
Use XSLT for this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="/container/item/xmlText/node()"/>
</xsl:template>
</xsl:stylesheet>
When this is applied on the provided XML document (corrected to be well-formed !!!):
<container>
<item>
<xmlText>
<someTag>
<otherTag>
Text
</otherTag>
</someTag>
</xmlText>
</item>
</container>
the wanted, correct result is produced:
<someTag>
<otherTag>
Text
</otherTag>
</someTag>
When this is your Element retrieved with XPath
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
Element element = (Element) xpath.evaluate(
"/container/item/xmlText", document, XPathConstants.NODE);
Then, you can do something along these lines:
java.io.ByteArrayOutputStream data =
new java.io.ByteArrayOutputStream();
java.io.PrintStream ps = new java.io.PrintStream(data);
// These classes are part of Xerces. But you will find them in your JDK,
// as well, in a different package. Use any encoding here:
org.apache.xml.serialize.OutputFormat of =
new org.apache.xml.serialize.OutputFormat("XML", "ISO-8859-1", true);
org.apache.xml.serialize.XMLSerializer serializer =
new org.apache.xml.serialize.XMLSerializer(ps, of);
// Here, serialize the element that you obtained using your XPath expression.
serializer.asDOMSerializer();
serializer.serialize(element);
// The output stream now holds serialized XML data, including tags/attributes...
return data.toString();
UPDATE
This would be more concise, rather than using Xerces internals. It's the same as Dimitre's solution, just not with an XSLT stylesheet but all in Java:
ByteArrayOutputStream out = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source source = new DOMSource(element);
Result target = new StreamResult(out);
transformer.transform(source, target);
return out.toString();

Categories

Resources