Java appending an element to XML document - java

I am trying to append an element to my xml document so it looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
</students>
However, it ends up looking like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students/>
This is the code I am using:
// results is the new XML document I created using DocumentBuilder.newDocument();
Element root = results.createElement("students");
results.appendChild(root);
How come it isn't looking like how I want it to?

Java dom is implemented based on the xml specification, and by definition: An element with no content is said to be empty : https://www.w3.org/TR/REC-xml/#sec-starttags.

Related

How to merge multple XML files and make proper namespaces in result XML

I have few XMLs needs to be merged and create a new xml. How i can extract the xml namespaces from the xmls and use it for resulting xml.
xml1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root1 xmlns:xlink="ABC/xlink" xmlns:xsi="XYZ/instance">
<tag1>123</tag1>
</root1>
xml2
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root2 xmlns:xlink2="EFG/xlink2" xmlns:xsi="XYZ/instance">
<tag2>321</tag2>
</root2>
expected result xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns:xlink="ABC/xlink" xmlns:xsi="XYZ/instance" xmlns:xlink2="EFG/xlink2">
<tag1>123</tag1>
<tag2>321</tag2>
</root>
Currently i am trying like below
Pattern pattern = Pattern.compile("xmlns[\\s\\S]*?>");
Matcher matcher = pattern.matcher(stringxml);
while(matcher.find()){
//storing the namespace.
}
Trying to do String manipulation to create the proper xml namespace. There are quite a few string operations happening to handle the namespace properly like need to avoid the duplicate namespace if its defined in both xmls. Is there any other better way to do it ?

Writing an XML in Matlab: How to add reference to DTD?

I'm trying to write an XML file using Matlab and I need to specify a DOCTYPE DTD at the header, but I haven't found any method for this in the Matlab documentation or questions related. Every question involving a DTD reference is about how to read an XML into Matlab.
What I am able to do now is an XML file of the type
<?xml version="1.0"?>
<root>
<child>
Hello world!
</child>
</root>
with the code
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
However, I need the file to include a DTD reference:
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "root.dtd" []>
<root>
<child>
Hello world!
</child>
</root>
Is there any function in com.mathworks.xml.XMLUtils for this? Or will I have to open the generated XML and manually insert the DTD reference?
You can stay with using the org.w3c.dom package: you can use the createDocumentType method of DOMImplementation.
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
With this update the full sample code is:
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
docNode.appendChild(doctype);
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
Output
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root PUBLIC "SYSTEM" "root.dtd">
<root>
<child>Hello World!</child>
</root>

Search child node in XML by sub-node content

I'm a noob whith XPath and I have a question: How can I search for a child in all branches by a particular name and display the information in the XML file?
<?xml version="1.0" encoding="UTF-8"?>
<movie><?xml version="1.0" encoding="UTF-8"?>
<movie>
<film>
<name>Peaceful Warrior</name>
<actor>Scott Mechlowicz</actor>
</film>
<film>
<name>Gone</name>
<actor>Scott Mechlowicz</actor>
</film>
<film>
<name>Gladiator</name>
<actor>Russel Crowe</actor>
</film>
</movie>
I tried:
//*[ancestor::movie/film[name = "Scott Mechlowicz"]]
But this shows all the information, when my intention is to show only movies in which that actor participates.
Is this possible with XPath or must I use getNameByTagName NodeList?
You can use XPath. If you are just trying to get a list of names of movies, try this XPath (get name of film where actor = "Scott Mechlowicz")
//film[actor = "Scott Mechlowicz"]/name
or
/movie/film[actor = "Scott Mechlowicz"]/name

How to Add Namespace to XML in java?

I have below xml. Namespace is missing in the below xml.
<?xml version="1.0" encoding="UTF-8"?>
<policy>
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
Looking for java code to take the above xml as input and add namespace (xmlns) element to it? The expected output xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<policy xmlns="http://aaa.bbb.com">
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
First of all, in the above xml, risk-policy-ind tag is not properly closed. In XML all tags are custom tags, they should close. Moreover,in xml, tags are performed without namespace.
If u just want to add xmlns attribute to policy tag, create policy element using w3c.dom.Element and set attribute using setAttribute function

Parse XML with nested xml opening tags <?xml ...?> in java

can you help me in parsing xml with nested <?xml version="1.0" encoding="utf-8"?> tags. when i am trying to parse this xml, i m getting parsing error.
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
</serviceResponse>
</soapenvBody>
</soap>
I don't think this is really a Java problem. Having a second XML declaration within the XML body is just illegal, so I don't think you'll be able to get any XML parsers to parse that. If you have control over the XML (it looks like you're generating it to store a response) then you could try wrapping the inner-XML document with CDATA:
<?xml version="1.0" encoding="utf-8"?>
<soap>
<soapenvBody>
<serviceResponse>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<data>
<respCode>0</respCode>
</data>
]]>
</serviceResponse>
</soapenvBody>
</soap>
EDIT:
I'm thinking that you most likely don't want the extra XML declaration inside that response at all. Do you have control over the code that creates the response? My guess is that the XML snippet <data>...</data> is created as a separate DOM object and then the string is spliced in the middle of the response. Writing out the entire XML document object results in the XML declaration being included, but if you just grab the document root node object (<data>) and write that out as a string then it probably won't include the extra XML declaration that's causing you all this trouble.
It occurred to me that a parser made for dealing with HTML might be able to do what you want. Since HTML tends to be a total mess compared to strict XML, HTML parsers are usually much more error-tolerant. A quick search turned up jsoup. I was able to pull the respCode from your sample XML above with roughly this code:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
String data = "your xml goes here";
Document doc = Jsoup.parse(data);
String respCodeRaw = doc.select("respCode").first().text();
int respCode = Integer.valueOf(respCodeRaw);
(I actually tested the library in the Clojure repl, but the code above should work!)
A tag that starts with like <? is a processing instruction. <?xml...> is an XML declaration, and can only be present at the beginning of the xml content. It's not allowed in the XML body.
Why does your soap body contain this? Do you have the option of removing it?
i did not find any parser in java to parse such embedded xml as it is not a valid xml and i guess almost all parses validate the xml before parsing it. so i choose the option to preprocess the xml and selected the inner xml then using SAX parser i parsed the xml and retrieved the values from xml. Guys thanks for your replies.

Categories

Resources