android simplexml parse elements with dynamic name - java

I need some advice how to parse this xml with java simplexml lib.
Problem is that i don't how how many items i will have in this xml element.
<Playoffs>
<PlayoffStatus>Potential Brackets</PlayoffStatus>
<LConference>Western Conference</LConference>
<RConference>Eastern Conference</RConference>
<LTeam1>Clippers (1)</LTeam1>
<LTeam2>Nuggets (8)</LTeam2>
<LTeam3>Grizzlies (4)</LTeam3>
<LTeam4>Warriors (5)</LTeam4>
<LTeam5>Spurs (3)</LTeam5>
<RTeam1>Heat (1)</RTeam1>
<RTeam2>Celtics (8)</RTeam2>
<RTeam3>Pacers (4)</RTeam3>
<RTeam4>Bulls (5)</RTeam4>
<RTeam5>Hawks (3)</RTeam5>
.......

You can use a Converter object or the #ElementListUnion interface. See here for an example.
https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/core/UnionInlineListWitinPathTest.java
https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/convert/HideEnclosingConverterTest.java

Related

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).

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

parsing XML to java variables(beans) using JAXB

Am working with the server response with my application. am getting this stuff as response as string.
<Body>
<HotelRQ xmlns="urn:Hotel_Search">
<POS>
<Source Username='USERNAME' Password='PASSWORD' PropertyID='PROPERTYID' />
</POS>
<AvailRequests>
<AvailRequest>
<StayDateRange Start='2009-09-05T12:00:00' End='2009-09-06T12:00:00'/>
<RoomStays>
<RoomStay> <!—for Room 1->
<GuestCounts>
<GuestCount Count='2'/>
</GuestCounts>
<ChildCounts>
<ChildAge Age='10'/>
<ChildAge Age='09'/>
</ChildCounts>
</RoomStay>
</RoomStays>
<SearchCriteria>
<Criterion>
<HotelRef HotelCityName='CITY' HotelName='' Area='' Attraction='' Rating=''/>
<Sorting Preference='2'/>
<ResponseType Compact="Y"/>
</Criterion>
</SearchCriteria>
</AvailRequest>
</AvailRequests>
</HotelRQ>
</Body>
can i use jaxb for this?
i am using this file to parse which i am getting on server response. what i nned to do for parsing it using JAXB. am getting it in String format.and how can i directly map into java class variables or beans?
thanx.
You can see this question: How do I load an org.w3c.dom.Document from XML in a string?
After loading the XML string inside a DOM document, you can call, among others, the method getElementsByTagName to retrieve specific tags and then iterate over them, using getNodeValue to extract the value inside a tag and getAttribute to extract attributes.
You can't easily map it to a Java class with desider fields at runtime, but you can wrap the parsing and extraction of elements inside a class with simple methods like getRooms()

Parsing a String representation of XML

From a String like
<TestData Identifier=\"Test\" requiredAttribute=\"Present\"></TestData> <TestData Identifier=\"Test1\" requiredAttribute=\"Present1\"></TestData> <TestData Identifier=\"Test2\" requiredAttribute=\"Present2\"></TestData> <TestData Identifier=\"Test3\" requiredAttribute=\"Present3\"></TestData>
whats the best way to get the values of the attributes requiredAttribute i.e (Present,Present1,Present2...)
You can look into JAXB unmarshalling. Check out this page for more details, it should point to what you need
http://jaxb.java.net/tutorial/section_3_1-Unmarshalling-and-Using-the-Data.html#Unmarshalling and Using the Data
For basic XML parsing like this, I've found NanoXML # http://nanoxml.cyberelf.be/ to be about the easiest and mostlightweight.
Working with XML in Java send you down a long road to pain if you start using all the other libraries.
That's not XML - but you could do it with regex or by converting it to XML and parsing it out. The latter is probably more expensive. It depends on what the actual test data is and your requirements for it.

Add xmlns="" in the xml

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

Categories

Resources