Add xmlns="" in the xml - java

I am getting response XML, in that I want to add xmlns attribute in each child node which is generated.
Present output:
<createProfileResponse xmlns="http://services.profile.webservices.ecaas.com">
<createProfileReturn>STRING</createProfileReturn>
</createProfileResponse>
Required output:
<createProfileResponse xmlns="http://services.profile.webservices.ecaas.com">
<createProfileReturn xmlns="">STRING</createProfileReturn>
</createProfileResponse>
How do I do this?
NOTE: I've used JAXB to generate the XML.

The problem is that you need to have "createProfileReturn" in the blank namespace, and you explicitly put the default namespace in a non-blank namespace in the surrounding tag.
If the XML parser is fully compliant you could create a "ecaaas" global namespace and use
<ecaas:createProfileResponse>
<createProfileReturn/>
</ecaas:createProfileResponse>

HIThanks for helping out, actually this we done in coding through the saopBinding class.
But we also modified the server-config.wsdd file I really didnt understand why we need wsdd file..
This gives only the service?.
Anil

Related

How to map unknown xml structure to java object using jaxb? eg.In three level xml structure: level-1 tag known, level-2 tag unknown, level-3 tag known

I am having below xml structure. And need to unmarshall using JAXB.
`
<Rule>
<RuleNumber>5001</RuleNumber>
**<RuleA>**
<Match> some text-1 </Match>
<Ignore> some text-2 </Ignore>
<MatchWord> some text-3 </MatchWord>
**</RuleA>**
**<RuleB>**
<Ignore> some text-4 </Ignore>
<MatchWord> some text-5 </MatchWord>
**</RuleB>**
</Rule>`
In the above xml , in level 1 <Rule> tag is there. It is known before unmarshalling.
In level -2 , <RuleNumber> , <RuleA> , <RuleB> tags are there. In this level-2 the Tag <RuleNumber> is known before unmarshalling but the other two tag's names are not known. i.e. <RuleA>, <RuleB> tags are not known before unmarshalling. And one more thing instead of <RuleA> or <RuleB> , the name of these tags may be any name. It may be <RuleC> or <RuleZ> or even <Rule101>, <Rule102>, <RuleA1> , <RuleZ1> etc.
In level-3 , <Match> , <Ignore>, <MatchWord> tags are there. Theses are known tags before unmarshalling.
I want to unmarshall this xml using jaxb . Here the name of the tag <RuleA> , <RuleB> are not known , the name can be anything. So how to write a class to map this kind of xml structure?. And subsequently needed to unmarshalling the data.
Is it possible to handle this scenario using JAXB ?
I know to write code for simple unknown type using #xmlAnyElement, i.e. if unknown tags are not having any child elements. But don't know how to approach this complex type. In my case the unknown tags having child tags.

xml to jaxb in xml cyclic references

How to convert following XML to java using jaxb
<work>
<subwork id="sub">
<ret="it">
</subwork>
<ret id="it">
<time>9</time>
</ret>
</work>
It is a bit tough since ret tag is outside subwork tag
Frst, you need to start with valid XML. I've made assumptions in correcting the XML:
<work>
<subwork id="sub">
<ret id="it"/>
</subwork>
<ret id="it">
<time>9</time>
</ret>
</work>
Second (and there are other ways of doing this), you need to create a schema that describes this XML. Without doing it for you, I'll say that the trick is to define an element, ret, and then refer to that element within the work element and again within the subwork element.
Third, you then feed that schema file (.XSD) into a tool that generates the JAXB classes. Typically this is xcj.exe (included with the Java JDK).

Problems using commons digester to parse subversion XML output

The Subversion XML formatted output is a combination of attribute and elements - particularly within the <path> element - see the sample below:
<?xml version="1.0" encoding="UTF-8"?>
<log>
<logentry
revision="29">
<author>bob</author>
<date>2013-02-14T17:21:42.848605Z</date>
<paths>
<path
action="A"
kind="dir"
copyfrom-path="/trunk"
copyfrom-rev="28">/tags/TAG-0.1</path>
</paths>
<msg>Creating tag TAG-0.1</msg>
</logentry>
</log>
I'm trying to use commons digester to parse this the log content into two different POJO's (LogEntry and Path) using the following:
ArrayList<LogEntry> logEntries = new ArrayList<LogEntry>();
digester.push(logEntries);
digester.addObjectCreate("*/logentry", LogEntry.class);
digester.addSetProperties("*/logentry");
digester.addBeanPropertySetter("*/logentry/author");
digester.addBeanPropertySetter("*/logentry/date");
digester.addBeanPropertySetter("*/logentry/msg");
digester.addSetNext("*/logentry", "add");
digester.addObjectCreate("*/logentry/paths/path", Path.class);
digester.addSetProperties("*/logentry/paths/path");
digester.addBeanPropertySetter("*/logentry/paths/path", "value");
digester.addSetNext("*/logentry/paths/path", "addPath");
(note addPath adds the path object being created onto an ArrayList<Path> within the created LogEntry object)
I can't figure out why the Path class is not being fully populated. Based upon the XML I can understand why the copyfrom-rev and copyfrom-path attributes might not be getting copied (due to the hyphen) into the corresponding copyFromRev attributes.
But I can't see any reason why the kind attribute isn't being set within the Path.
Does anyone have any ideas?
I need to use a digester.addSetProperties() call to get the copyfrom-path and copyfrom-rev attributes populated:
digester.addSetProperties("*/logentry/paths/path", "copyfrom-path", "copyfrompath");
digester.addSetProperties("*/logentry/paths/path", "copyfrom-rev", "copyfromrev");
However for some reason the kind attribute still isn't being populated.

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

Retrieve value of attribute using XPath

I am trying to retrieve the value of an attribute from an xmel file using XPath and I am not sure where I am going wrong..
This is the XML File
<soapenv:Envelope>
<soapenv:Header>
<common:TestInfo testID="PI1" />
</soapenv:Header>
</soapenv:Envelope>
And this is the code I am using to get the value. Both of these return nothing..
XPathBuilder getTestID = new XPathBuilder("local-name(/*[local-name(.)='Envelope']/*[local-name(.)='Header']/*[local-name(.)='TestInfo'])");
XPathBuilder getTestID2 = new XPathBuilder("Envelope/Header/TestInfo/#testID");
Object doc2 = getTestID.evaluate(context, sourceXML);
Object doc3 = getTestID2.evaluate(context, sourceXML);
How can I retrieve the value of testID?
However you're iterating within the java, your context node is probably not what you think, so remove the "." specifier in your local-name(.) like so:
/*[local-name()='Header']/*[local-name()='TestInfo']/#testID worked fine for me with your XML, although as akaIDIOT says, there isn't an <Envelope> tag to be seen.
The XML file you provided does not contain an <Envelope> element, so an expression that requires it will never match.
Post-edit edit
As can be seen from your XML snippet, the document uses a specific namespace for the elements you're trying to match. An XPath engine is namespace-aware, meaning you'll have to ask it exactly what you need. And, keep in mind that a namespace is defined by its uri, not by its abbreviation (so, /namespace:element doesn't do much unless you let the XPath engine know what the namespace namespace refers to).
Your first XPath has an extra local-name() wrapped around the whole thing:
local-name(/*[local-name(.)='Envelope']/*[local-name(.)='Header']
/*[local-name(.)='TestInfo'])
The result of this XPath will either be the string value "TestInfo" if the TestInfo node is found, or a blank string if it is not.
If your XML is structured like you say it is, then this should work:
/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='TestInfo']/#testID
But preferably, you should be working with namespaces properly instead of (ab)using local-name(). I have a post here that shows how to do this in Java.
If you don't care for the namespaces and use an XPath 2.0 compatible engine, use * for it.
//*:Header/*:TestInfo/#testID
will return the desired input.
It will probably be more elegant to register the needed namespaces (not covered here, depends on your XPath engine) and query using these:
//soapenv:Header/common:TestInfo/#testID

Categories

Resources