adding mutiple namespace for a element in xml file using dom4j - java

The XML file which need to generate:
<?xml version="1.0" encoding="UTF-8" ?>
<wrapper:MMSRMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wrapper="urn:iso:std:iso:20022:tech:xsd:head.003.001.01" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd">
<header:AppHdr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:header="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
<header:Fr>
...
</header:Fr>
</header:AppHdr>
</wrapper:MMSRMessage>
Two namespaces were added for the root element "wrapper:MMSRMessage",It has no problem.
The following is the Java code for it:
Document document = DocumentHelper.createDocument();
Element wrapper = document.addElement("wrapper:MMSRMessage");
wrapper.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
.addNamespace("wrapper", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01")
.addAttribute("xsi:schemaLocation", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd");
However, when I add two namespaces for element "header:AppHdr", I get the error message:
Exception in thread "main" org.dom4j.IllegalAddException: No such namespace prefix
using java code:
Element headerApp = wrapper.addElement("header:AppHdr");
headerApp.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
.addNamespace("header", "urn:iso:std:iso:20022:tech:xsd:head.001.001.01");
I also have tried so:
Element headerApp = wrapper.addElement("header:AppHdr","urn:iso:std:iso:20022:tech:xsd:head.001.001.01")
.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
in this way the error does not occur, but the namespace "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" can not be added for the element "header:AppHdr".
That's my first question at Stackoverflow. I hope I can get an answer hier :-)

DOM4J generally offers too many ways to skin the cat. In this area, the confusion is increased because Document#addElement(String, String) and Element#addElement(String, String) do very different validations: In the first case you can add an element with a qualified name without having the prefix bound to a namespace and the element ends up having no namespace (this is a bug). In the second case you must have the prefix bound (correct).
All in all, I reccomend not using qualified element and attribute names (prefix:local-name) if you can avoid it. Instead, strictly separate the local name of the element or attribute and use properly declared Namespace and QName constructs. In your case:
Document document = DocumentHelper.createDocument();
Namespace xsi = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Namespace wrapper = Namespace.get("wrapper", "urn:iso:std:iso:20022:tech:xsd:head.003.001.01");
Namespace header = Namespace.get("header", "urn:iso:std:iso:20022:tech:xsd:head.001.001.01");
Element wrapperElement = document
.addElement(new QName("MMSRMessage", wrapper))
.addAttribute(new QName("schemaLocation", xsi), "urn:iso:std:iso:20022:tech:xsd:head.003.001.01 MMSR_head.003.001.01_Wrapper.xsd");
Element headerApp = wrapperElement.addElement(new QName("AppHdr", header));
headerApp.addElement(new QName("Fr", header));

Related

Java 9, INVALID_CHARACTER_ERR when trying to add URL as element in XML

I'm working with XML for the first time, trying to generate XML to send over to a client and I'm having a hell of a time doing it. Whenever I try to pass a URL, I get an INVALID_CHARACTER_ERR and nothing I've tried so far works.
I tried using replacements like & #123; and so on for the curly braces, and tried escaping everything that wasn't a letter, resulting in the abomination under my code. It seems to throw the error if I have any kind of character that isn't a letter. Another thing that I noticed is that the document's InputEncoding is null, but that seems to be because I'm creating it in code, does that mean that it actually doesn't have an encoding type? I haven't been able to find an easy way to set it either.
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document orders = dBuilder.newDocument();
Element order = orders.createElement("{https://secure.targeturl.com/foo/bar}tagpayload");
Element tOrder = orders.createElement("tagorder");
order.appendChild(tOrder);
Element header = orders.createElement("orderheader");
tOrder.appendChild(header);
Element billto = orders.createElement("billto");
header.appendChild(billto); ```
``` "& #123;https& #58;& #47;& #47;secure& #46;targeturl& #46;com/foo& #47;bar& #125;tagpayload" ```
This is not the correct way to create a namespaced element:
Element order = orders.createElement("{https://secure.targeturl.com/foo/bar}tagpayload");
Instead, use the createElementNS method:
Element order = orders.createElementNS("https://secure.targeturl.com/foo/bar", "tagpayload");
You are seeing an exception because { is not a legal character in an XML element name. createElement has no awareness of namespaces or the “{uri}name” namespace notation.

How to get Node from XML without considering namespace name in Java?

I am writing a java program in which I am parsing input xml file which looks like this:
...
<ems:DeterminationRequest>
<ems:MessageInformation>
<ns17:MessageID xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">1000225404</ns17:MessageID>
<ns17:MessageTimeStamp xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">2015-07-28T01:17:04</ns17:MessageTimeStamp>
<ns17:SendingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">CH</ns17:SendingSystem>
<ns17:ReceivingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">LD</ns17:ReceivingSystem>
<ns17:ServicingFipsCountyCode xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">037</ns17:ServicingFipsCountyCode>
</ems:MessageInformation>
</ems:DeterminationRequest>
...
Now I am trying to get node "ems:MessageInformation" without considering namespace name "ems". So I tried following lines of code:
Document doc = db.parse(new FileInputStream(new File("D:\\test.xml")));
Node element = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation").item(0);
System.out.println(element.getNodeName());
But it's giving Null Pointer exception because function is not reading required node. I gone through this link for reference. Can someone tell me what I am doing wrong here?
This is an odd/buggy behaviour in den NodeList implementation returned by
doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation")
It allows you to access item(0) but returns a null object.
(If you are using a current JDK the NodeList implementation is com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl which lazily loads its items and shows this buggy behaviour).
To prevent the NullPointerException you should first check if the returned NodeList has a length > 0:
NodeList result = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation");
if (result.getLength() > 0) {
Node element = (Element)result.item(0);
...
}
Then you need to find out why getElementsByTagNameNS does not return the element.
One possible reason could be that you parsed the document without namespace support. The consequence is that the dom elements don't have namespace information and getElementsByTagNameNS fails.
To turn on namespace support use:
DocumentBuilderFactory.setNamespaceAware(true);
Alternatively without namespace support you could search for
NodeList nl = doc.getDocumentElement().getElementsByTagName("ems:MessageInformation");

Xpath and namespace, select node iterator

I have this kind of XML:
<?xml version="1.0" encoding="UTF-8"?>
<documentAnswer xmlns="http://schemas.pkh.hr/doc/2013/documents/rda_30" domainName="rda" domainVersion="3.0">
<answerTimestamp>2013-08-21T13:35:25.894</answerTimestamp>
<correct>
<docId>RDA2_29F81D27-1409BE49E2E-7FF8</docId>
<attachments>
<attachment>
<format>application/pdf</foraat>
<generatedType>StampanNaBlanko</generatedType>
<encodedPdf>JVBERiYKJSVFT0YK</encodedPdf>
</attachment>
</attachments>
</correct>
Those attachment tags can be one or more. I am trying to get NodeIterator of attachment tags in java using
XPathAPI.selectNodeIterator(node,
"/documentAnswer/correct/attachments/attachment")
and then iterate, but I am not succeeding. I am guessing that problem is in xpath and that it is related to namespace, but don't know how to solve it. I tried with this kind of xpath but no success:
XPathAPI.selectNodeIterator(node,
"/rda:documentAnswer/correct/attachments/attachment", "rda",
"http://schemas.pkh.hr/doc/2013/documents/rda_30")
/rda:documentAnswer/correct/attachments/attachment
Here you are assigning the rda namespace only to the root element, but it is declared in the XML as the default namespace. Therefore, all your elements are in that namespace. You must use
/rda:documentAnswer/rda:correct/rda:attachments/rda:attachment
Unfortunately, the XPath standard does not support the declaration of a default namespace.
3 kind of approach :
A-Embed namespace for query QNames and not only local-names()
with corrected header :
<ns0:documentAnswer xmlns:ns0="http://schemas.pkh.hr/doc/2013/documents/rda_30" domainName="rda" domainVersion="3.0">
xpath
/ns0:documentAnswer
B-Query only local-names with local-name() function
/*[local-name()="documentAnswer"]
C-Query local-names and namespaces (querying QNames)
/*[local-name()="documentAnswer" and namespace-uri()='http://schemas.pkh.hr/doc/2013/documents/rda_30']
see http://nealwalters.blogspot.fr/2005/01/common-xpath-examples-questions-issues.html
If you prefer you can get it ignoring the namespaces this way:
/*:documentAnswer/*:correct/*:attachments/*:attachment

Groovy: how to parse xml and preserve namespaces and schemaLocations

I'm trying to use groovy to simply add a node to a at a particular location. My source schema looks like this
<s1:RootNode
xmlns:s1="http://localhost/s1schema"
xmlns:s2="http://localhost/s2schema"
xsi:schemaLocation="http://localhost/s1schema s1schema.xsd
http://localhost/s2schema s2schema.xsd">
<s1:aParentNode>
<s2:targetNode>
<s2:childnode1 />
<s2:childnode2 />
<s2:childnode3 />
<s2:childnode4 />
</s2:targetNode>
</s1:aParentNode>
</s1:RootNode>
I'd like to simply add a new child node inline with the other ones to make the output
<s1:RootNode
xmlns:s1="http://localhost/s1schema"
xmlns:s2="http://localhost/s2schema"
xsi:schemaLocation="http://localhost/s1schema s1schema.xsd
http://localhost/s2schema s2schema.xsd">
<s1:aParentNode>
<s2:targetNode>
<s2:childnode1 />
<s2:childnode2 />
<s2:childnode3 />
<s2:childnode4 />
<s2:childnode5 >value</s2:childnode5>
</s2:targetNode>
</s1:aParentNode>
</s1:RootNode>
To do this i have the following simple groovy script
def data = 'value'
def root = new XmlSlurper(false,true).parseText( sourceXML )
root.'aParentNode'.'topNode'.appendNode{
's2:childnode5' data
}
groovy.xml.XmlUtil.serialize(root);
however when i do this the namespaces and schemaLocations that are applied to the root node are being removed. and the namespace, but not the schema location is being added to each of the child nodes.
this is causing validation issues downstream.
How do i simply process this xml. perform no validation and leave the xml as is and add a single node of a namespace i specify?
One note: we process many messages and i won't know in advance the outer most namespace (s1 in the above example) but even with that, i'm really just lookign for a technique that is a "dumber" processing of xml
Thanks!
First, I had to add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" to define your xsi namespace. Without it I would receive a SAXParseException for the unbound xsi prefix.
Additionally, I consulted this question on successfully appending a namespaced xml node to an existing document.
Finally, we had to utilize the StreamingMarkupBuilder to work around the moving of the namespaces. Bascially, by default the serializer moves the referenced namespaces to the first node that actually uses the namespace. In your case it was moving your s2 namespace attribute to the "targetNode" tag. The following code produces the results you want, but you will still have to know the correct namespaces to use to instantiate the StreamingMarkupBuilder.
def root = new XmlSlurper(false, true).parseText( sourceXML )
def data = '<s2:childnode5 xmlns:s2="http://localhost/s2schema">value</s2:childnode5>'
def xmlFragment = new XmlSlurper(false, true).parseText(data)
root.'aParentNode'.'targetNode'.appendNode(xmlFragment);
def outputBuilder = new StreamingMarkupBuilder()
String result = XmlUtil.serialize(outputBuilder.bind {
mkp.declareNamespace('s1':"http://localhost/s1schema")
mkp.declareNamespace('s2':"http://localhost/s2schema")
mkp.yield root }
)
XMLSlurper (or XMLParser) does not handle namespaces if you set the second parameter of the constructor:
XmlSlurper (boolean validating, boolean namespaceAware)
to false:
def root = new XmlSlurper(false, false).parseText( sourceXML )
Without setting namespaceAware to false, I also faced strange bahavior of the parser. After setting to false, it leaves the XML as is, with no namespace changes.

Reducing code redundancy while creating XML with XOM

I am using XOM as my XML parsing library. And i am using this for creating XML also. Below is the scenario described with example.
Scenario:
Code:
Element root = new Element("atom:entry", "http://www.w3c.org/Atom");
Element city = new Element("info:city", "http://www.myinfo.com/Info");
city.appendChild("My City");
root.appendChild(city);
Document d = new Document(root);
System.out.println(d.toXML());
Generated XML:
<?xml version="1.0"?>
<atom:entry xmlns:atom="http://www.w3c.org/Atom">
<info:city xmlns:info="http://www.myinfo.com/Info">
My City
</info:city>
</atom:entry>
Notice in the XML that here info namespace is added with the node itself. But I need this to be added in root element. like below
<?xml version="1.0"?>
<atom:entry xmlns:atom="http://www.w3c.org/Atom" xmlns:info="http://www.myinfo.com/Info">
<info:city>
My City
</info:city>
</atom:entry>
And to do that, i just need following piece of code
Element root = new Element("atom:entry", "http://www.w3c.org/Atom");
=> root.addNamespaceDeclaration("info", "http://www.myinfo.com/Info");
Element city = new Element("info:city", "http://www.myinfo.com/Info");
... ... ...
Problem is here i had to add http://www.myinfo.com/Info twice. And in my case there are hundreds of namespaces. So there will so too much redendancy. Is there any way to get rid of this redundancy?
No, there is no way to get rid of this redundancy and that's a deliberate decision. In XOM the namespace is a fundamental part of the element itself, not a function of its position in the document.
Of course you could always declare a named constant for the namespace URI.

Categories

Resources