Setting attribute without prefix using Java DOM - java

Using Java DOM I'm trying to set an attribute for an element without the namespace before the attribute name.
So, what I need is:
<documentObject xmlns="http://www.myschema.com">
<element1 attr1="value">foo</element1>
</documentObject>
If I try to set the attribute as following element1.setAttributeNS("http://www.myschema.com", "attr1", value); I get an empty xmlns tag and additionaly a xmlns with prefix like the following:
<element1 attr1="value" xmlns="" xmlns:ns3="http://www.myschema.com">foo</element1>
If I try to set the attribute as following element1.setAttribute("xmlns:attr1", value); I get a prefix (xmlns) before my attribute name as shown here:
<element1 xmlns:attr1="value">foo</element1>
As for further information I create my elements as following:
Element element = dom.createElementNS("http://www.myschema.com", elemName);
element.appendChild(dom.createCDATASection("foo");
xmlElement.appendChild(element);

Let's look at your desired output again:
<documentObject xmlns="http://www.myschema.com">
<element1 attr1="value">foo</element1>
</documentObject>
In this document, the following statements are true:
documentObject and element1 are in the http://www.myschema.com namespace.
The attribute attr1 is not in any namespace.
While elements whose names are not prefixed are going to be in whatever default namespace is in effect, attributes whose names are not prefixed are not in any namespaces. See the spec:
Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
So to obtain the output you desire, you should be able to just do:
element1.setAttribute("attr1", value);
Of course this all depends on the desired output being correct. If really attr1 must be in a namespace, then your desired output is incorrect.

Below code will produce output
<?xml version="1.0" encoding="UTF-8"?>
<documentObject xmlns="http://www.myschema.com">
<element1 attr1="value">foo</element1>
</documentObject>
Java Code
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XMLTest {
public static void main(String[] args) throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element element = doc.createElementNS("http://www.myschema.com", "documentObject");
doc.appendChild(element);
Element element1 = doc.createElement("element1");
element.appendChild(element1);
element1.appendChild(doc.createTextNode("foo"));
Attr attr = doc.createAttribute("attr1");
attr.setValue("value");
element1.setAttributeNode(attr);
element.appendChild(element1);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("testfile.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
}
}

Related

Problem with inserting/appending comment before root node and after prolog with org.w3c.dom

Im trying to insert a comment with syntax version information before the first node of an xml document but for some reason it always gets appended after the last element. I cant use other libraries like XStream and such.
I have already tried to do this via the append and the insertBefore method but both of them yield the same result. Is it possible to do this just with the plain Document, Comment and Node or do I have to use a Transformer?
Document doc = XmlDocumentUtil.createDocument();
//Create root node
Node rootNode = doc.createElement(NODE_DATA_CONFIGURATION);
doc.appendChild(rootNode);
//Create syntax identification comment
Comment syntaxIdentification = doc.createComment(writeSyntaxIdentificationQE);
doc.insertBefore(syntaxIdentification, rootNode);
//Create revision information
Element modificationNumber = doc.createElement("modificationNumber");
modificationNumber.setTextContent(String.valueOf(configTable.getModificationNumber()));
rootNode.appendChild(modificationNumber);
Element modificationTime = doc.createElement("modificationTime");
modificationTime.setTextContent(configTable.getModificationTime());
rootNode.appendChild(modificationTime);
Element modifier = doc.createElement("modifier");
modifier.setTextContent(configTable.getModifier());
rootNode.appendChild(modifier);
for (...) {
... some data generation
}
What I get:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DataConfiguration>
<modificationNumber>2</modificationNumber>
<modificationTime>2019-07-25 07:42:28.804 +0200</modificationTime>
<modifier>testuser</modifier>
<someData idxFrom="4" idxTo="9"
signalName="1" signalOffset="273.15" signalTemplate="asdf" skip="Y"/>
</DataConfiguration>
<!--SyntaxIdentification: 1.0-->
What I need:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--SyntaxIdentification: 1.0-->
<DataConfiguration>
<modificationNumber>2</modificationNumber>
<modificationTime>2019-07-25 07:42:28.804 +0200</modificationTime>
<modifier>testuser</modifier>
<someData idxFrom="4" idxTo="9"
signalName="1" signalOffset="273.15" signalTemplate="asdf" skip="Y"/>
</DataConfiguration>
To me your code seems good overall. Seems like you are missing only text nodes.
Here is full working code.
package xml;
import java.io.StringWriter;
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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class XMLComment {
public static void main(String[] args) throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
// Create root node
Node rootNode = doc.createElement("DataConfiguration");
doc.appendChild(rootNode);
// Create syntax identification comment
Comment syntaxIdentification = doc.createComment("SyntaxIdentification: 1.0");
doc.insertBefore(syntaxIdentification, rootNode);
// Create modificationNumber
Node modificationNumber = doc.createElement("modificationNumber");
Text mnText = doc.createTextNode("123456");
modificationNumber.appendChild(mnText);
// Create modificationTime
Element modificationTime = doc.createElement("modificationTime");
Text mtText = doc.createTextNode("2019-07-25 07:42:28.804 +0200");
modificationTime.appendChild(mtText);
rootNode.appendChild(modificationNumber);
rootNode.appendChild(modificationTime);
printXML(doc);
}
private static void printXML(Document doc) throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
}
}
It will output, close to what exactly you are looking for.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--SyntaxIdentification: 1.0--><DataConfiguration>
<modificationNumber>123456</modificationNumber>
<modificationTime>2019-07-25 07:42:28.804 +0200</modificationTime>
</DataConfiguration>
Hope it helps.

Java: Append to XML File [duplicate]

This question already has answers here:
Modify the content of a file using Java
(6 answers)
Closed 5 years ago.
I have the below code that writes to an xml file, the problem that I am having is that it creates a new file every time i write to it and overwrites the other data saved in the file. I am looking for a solution that would append to the existing file instead. How do I modify this code to append to the file each time instead of overwrite? Also, I am using the netbeans IDE to run this program.
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.xml.sax.SAXException;
public class WriteXMLFile {
public static void main() throws ParserConfigurationException,SAXException,Exception
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();//
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();//
Document doc = docBuilder.newDocument();// this is difrent
Element rootElement = doc.createElement("Contacts");//
doc.appendChild(rootElement); // this is difrent
// staff elements
Element Contact1 = doc.createElement("Contact1");
rootElement.appendChild(Contact1);
// set attribute to staff element
Contact1.setAttribute("id","1");
// firstname elements
Element firstname = doc.createElement("Name");
firstname.appendChild(doc.createTextNode(EmailFrame.name.getText()));
Contact1.appendChild(firstname);
//Email Element
Element email = doc.createElement("Email");
email.appendChild(doc.createTextNode(EmailFrame.email.getText()));
Contact1.appendChild(email);
// phone element
Element phone= doc.createElement("Phone");
phone.appendChild(doc.createTextNode(EmailFrame.phone.getText()));
Contact1.appendChild(phone);
//id element
Element id = doc.createElement("ID");
id.appendChild(doc.createTextNode(EmailFrame.id.getText()));
Contact1.appendChild(id);
try{
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:/Users/steve/Desktop/xmlemail/Email.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
}
catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
What you need to do is read the contents of the xml file into an object first before writing, then append your new content to your object then write your object to the xml file
Look at this resource for reading an xml file it should help
https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

How I need to get tag name of sub child in xml by comparing the attribute value and given string value using Java?

I have a xml file. I need to get the sub child tag of the parent tag (Body) in xml file using Java. First I need to use DOM for reading an element
and get xml file from my local machine drive. I have one String varaible (Sring getSubChildValue = "181_paragraph_13") and I need to compare the value
with each and every attribute Value in the Xml file. If the given Value may be in sub child tag,I cont able to get a Value.
what I need to do for compare the String variable and with Xml File
What I need to do for print the Tag name if the String value is equal to any attrinbute Value.
Example: (P) Tag is the sub child of Tag (Body) which contain the given String Value. So I need to get tag name P.
How to avoid the Hard coding the sub-child Name to get the solution?
Example XML file:
<parent>
<Body class="student" id="181_student_method_3">
<Book class="Book_In_School_11" id="181_student_method_11"/>
<subject class="subject_information " id="181_student_subject_12"/>
<div class="div_passage " id="181_div_method_3">
<p class=" paragraph_book_name" id="181_paragraph_13">
<LiberaryBook class="Liberary" id="181_Liberary_9" >
<Liberary class="choice "
id="Liberary_replace_1" Uninversity="University_Liberary_1">
Dubliners</Liberary>
<Liberary class="choice "
id="Liberary_replace_2" Uninversity="University_Liberary_2">
Adventure if sherlock Holmes</Liberary>
<Liberary class="choice "
id="Liberary_replace_3" Uninversity="University_Liberary_3">
Charlotte’s Web</Liberary>
<Liberary class="choice "
id="Liberary_replace_4" Uninversity="University_Liberary_4">
The Outsiders</Liberary>
</LiberaryBook>
</p>
</div>
</Body>
</parent>
Example Java code:
import java.io.File;
import java.io.IOException;
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.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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class PerfectTagChange {
public static void main(String[] args) {
String filePath = "/xmlfile/Xml/check/sample.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory
dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
changeValue(root,doc);
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/xmlfile/Xml/check/Demo.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");
} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
//This Method is used to check which attribute contain given string Value : Hard code parent tag, But no other tag.
private static void changeValue(Node someNode,Document doc) {
Sring getSubChildValue = "181_paragraph_13"
NodeList childs = someNode.getChildNodes();
for (int in = 0; in < childs.getLength();) {
Node child = childs.item(in);
if (child.getNodeType() == Document.ELEMENT_NODE) {
if (child.getNodeName().equalsIgnoreCase("Body") ) {
//If I hard code the ID here on getNamedItem("id"),
If the attribute Name got Changed from ID to Name
it will be in problem.
//3.What is the solution for solving the problem.
if(child.getAtrribute.getNamedItem("id").getNodeValue().equals(getSubChildValue)){
system.out.println(child.getAtrribute.getNamedItem("id").getNodeValue());
}
}
}
}
}
If you change your code to this:
private static void changeValue(Node someNode, Document doc, String searchString) throws Exception {
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.evaluate("//*[#*=\"" + searchString + "\"]",
doc.getDocumentElement(),
XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("Tagname: " + nodes.item(i).getNodeName());
}
}
you don't have the name of the attribute to be hardcoded.
EDIT:
Added searchString as parameter.

Transform XML with XSLT in Java using DOM

I've been looking around everywhere for Java samples to transform an XML document with an XSLT. I've found several samples using new File("path/to/file.xml") to load in the XML and the XSLT and those work great. My problem is that I'm trying to use this in a new method that will accept two org.w3c.dom.Document objects. As soon as I replace the StreamSource used to load in the XSLT with a DOMSource the result of my call is then the XSLT instead of the transformed XML.
Working code from How to call XSL template from java code?:
Source xmlInput = new StreamSource(new File("c:/path/to/input.xml"));
Source xsl = new StreamSource(new File("c:/path/to/file.xsl"));
Result xmlOutput = new StreamResult(new File("c:/path/to/output.xml"));
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.transform(xmlInput, xmlOutput);
} catch (TransformerException e) {
// Handle.
}
My code:
public static Document transformXML(Document xml, Document xslt) throws TransformerException, UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException, FactoryConfigurationError{
Source xmlSource = new DOMSource(xml);
Source xsltSource = new DOMSource(xslt);
StreamResult result = new StreamResult(new StringWriter());
// the factory pattern supports different XSLT processors
TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);
Document resultDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(result.getWriter().toString().getBytes("utf-8")));
return resultDoc;
}
My result document is then the XSLT instead of the XML. What am I doing wrong with the DOMSource?
XSLT and XPath only make sense with a namespace aware DOM implementation and DOM tree, that is why I asked "Are the DOM trees you feed to the transformer built with a namespace aware document builder?" in my comment.
As far as I have tested with Oracle Java 1.8, when a not namespace-aware DocumentBuilderFactory and the built-in Transformer is used, your method returns the stylesheet code. However as soon as I change the DocumentBuilderFactory to be namespace aware, the result is as intended.
Here is the working sample:
package domsourcetest1;
import java.io.IOException;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
/**
*
* #author Martin Honnen
*/
public class DOMSourceTest1 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document xslt = db.parse("sheet1.xsl");
Document xml = db.newDocument();
xml.appendChild(xml.createElementNS(null, "root"));
Document result = transformXML(xml, xslt);
System.out.println(result.getDocumentElement().getTextContent());
LSSerializer serializer = ((DOMImplementationLS) xml.getImplementation()).createLSSerializer();
System.out.println(serializer.writeToString(result));
}
public static Document transformXML(Document xml, Document xslt) throws TransformerException, ParserConfigurationException, FactoryConfigurationError {
Source xmlSource = new DOMSource(xml);
Source xsltSource = new DOMSource(xslt);
DOMResult result = new DOMResult();
// the factory pattern supports different XSLT processors
TransformerFactory transFact
= TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);
Document resultDoc = (Document) result.getNode();
return resultDoc;
}
}
The sample stylesheet simply outputs information about the XSLT processor:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<debug>
<xsl:value-of select="system-property('xsl:vendor')"/>
</debug>
</xsl:template>
</xsl:stylesheet>
Output of the program is
Apache Software Foundation (Xalan XSLTC)
<?xml version="1.0" encoding="UTF-16"?>
<debug>Apache Software Foundation (Xalan XSLTC)</debug>
Now when I comment out //dbf.setNamespaceAware(true); in the main method the result is
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><debug><xsl:value-of select="system-property('xsl:vendor')"/></debug></xsl:template></xsl:stylesheet>
meaning the result is indeed the stylesheet document. That is obviously a bug or at least a quirk with the built-in Xalan Transformer, when I put Saxon 6.5.5 on the class path the problem does not occur, nor does it occur with Saxon 9.6 on the class path.
In general, however, I don't think you will get meaningful results when using XSLT or XPath with not namespace aware DOM trees. See also the DOM2DOM sample in the Xalan release http://svn.apache.org/viewvc/xalan/java/tags/xalan-j_2_7_2/samples/DOM2DOM/DOM2DOM.java?revision=1695338&view=markup which says
// And setNamespaceAware, which is required when parsing xsl files
dFactory.setNamespaceAware(true);

XML processing instructions

The following code
import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("out.xml"));
xsr.nextTag(); // Advance to statements element
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
t.transform(new StAXSource(xsr), new StreamResult("result.txt"));
}
}
}
produces some txt, and on the first line of it there are processing instructions (<?xml version="1.0">). Where do they come from and how to get rid of them? And how to manipulate them?
You can disable the creation of the xml declaration by setting the output properties on the transformer:
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
If you want to generate text files from the xml then you probably want to specify an XSLT source in the TransformerFactory.newInstance call and set the output properties in that XSLT.
The line <?xml version="1.0"?> is an xml declaration. According to the W3C XML Recommendation
XML documents SHOULD begin with an XML declaration which specifies the version of XML being used.
it is supposed to be there (albeit not mandatory).
Btw. in a strict technical sense it is not a processing instruction.
Edit: the encoding can be changed with:
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

Categories

Resources