How can i parse the following XML using JDOM - java

I have an XML document as follows:
<?xml version="1.0" encoding="UTF-8"?>
<decision>
<question id="0">
<questionText>What type is your OS?</questionText>
<answer id="0">
<answerText>windows</answerText>
</answer>
<answer id="1">
<answerText>linux</answerText>
</answer>
<answer id="2">
<answerText>mac</answerText>
</answer>
</question>
<question id="1">
<questionText>What are you looking for?</questionText>
<answer id="0">
<answerText>table</answerText>
<question id="0">
<questionText>Which color table you want?</questionText>
<answer id="0">
<answerText>green</answerText>
</answer>
<answer id="1">
<answerText>black</answerText>
</answer>
<answer id="2">
<answerText>pink</answerText>
</answer>
</question>
</answer>
<answer id="1">
<answerText>chair</answerText>
</answer>
<answer id="2">
<answerText>bed</answerText>
</answer>
<answer id="3">
<answerText>cloth</answerText>
</answer>
</question>
Now I want to parse the above XML using jdom in Java. It kind of recursive and important thing to note is a Question can't be a direct child of Question and same applies for Answer.

Article
In the light of previous related questions, I'd like to repeat and stress the advice of others (like JB Nizet commented on this question):
Learn Java, learn XML, pick the tools and API's you need for your project and learn to use those too. If at one point you get into trouble, everybody here will be happy to help you out debugging your code.
I'm aware that this may seem harsh but it gets to the point where your program gets built by StackOverflow users and not yourself.
That being said, the link at the top of this answer leads to a tutorial on using JDOM to traverse your XML.

Use Element.getChildren(String) to get all of the question tags and loop through that List - calling getChildren(String) to get all of the answers, or getChild(String) if there can be only one child element.

first that you need use is XSD to validate the XML.

Related

JSON to XML with type attribute in java

I'm rewriting C# application into java code.
There is REST API which return jsons.
I have to parse json to XML but C# library and Java doing it in difference ways.
How to keep type= attribute in java? I can't use JAXB annotations becouse there are too many objects in response and they might changing. XML.toString(jsonObject) doesn't work for me.
C# parsing is done in this way:
XDocument.load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonString), new XmlDictionaryReaderQuotas()));
C# result:
<root type="object">
<Items type="array">
<item type="object">
<Name type="string">test</Name>
<Total type="number">12.8000000</Total>
<CurrencyCode type="string">CHF</CurrencyCode>
<Country type="string">CH</Country>
</item>
</Items>
</root>
Java result:
<root>
<Items>
<item>
<Name>test</Name>
<Total>12.8000000</Total>
<CurrencyCode>CHF</CurrencyCode>
<Country>CH</Country>>
</item>
</Items>
</root>
I've used org.w3c.Document and org.w3c.dom.Element and set up attribute "type".
Anyway thanks for help :)

Splitting nested XML with Camel Tokenizer language

I am trying to split following XML with Camel's XMLTokenizer language:
<units>
<unit type="menu">
<id>1</id>
<unit type="submenu">
<id>1</id>
</unit>
</unit>
<unit type="menu">
<id>2</id>
<unit type="submenu">
<id>1</id>
</unit>
</unit>
</units>
My splitter looks like this:
.split().tokenizeXML("unit").streaming()
and my problem is that it is producing splitted XML without end tag like this:
<unit type="menu">
<id>1</id>
<unit type="submenu">
<id>1</id>
</unit>
It would appear that with tokenizeXML() it is not possible to get this working since it will just scan for </unit> end tag. What would be preferred way to handle this case? Is there some other splitting method that would get me the result that I need? I would like to use streaming() so splitting with xpath() is not an option for me.
You can use camel-stax that allows to use the SAX api that supports streaming mode.
http://camel.apache.org/stax
You would need to define a POJO and the JAXB annotations that declares the binding.

Mule iterate over xpath result

I've set the following xml as payload in order to iterate over every product using splitter component.
<root>
<product>
<id>1</id>
<name>apple</name>
</product>
<product>
<id>2</id>
<name>orange</name>
</product>
</root>
<splitter expression="#[xpath('//product')]" />
The splitter component returns an object of type org.dom4j.tree.DefaultElement on which I call the method asXML() to get single product's xml.
First iteration
<product>
<id>1</id>
<name>apple</name>
</product>
Second iteration
<product>
<id>2</id>
<name>orange</name>
</product>
I need to replace splitter with foreach component, but I'm having some troubles.
<foreach collection="#[xpath('//product')]">
...
</foreach>
The foreach component returns an object of type org.apache.xerces.dom.ElementNSImpl which hasn't the method asXML().
Any idea how I can get products'xml as String as explained in the first example?
Thanks in advice!
Use Mule's dom-to-xml-transformer.
Reference: http://www.mulesoft.org/documentation/display/current/DomToXml+Transformer

Build the XML Document object using JDBC ResultSet

We are getting Cursor in JDBC ResultSet object.Now i have to build the complex XML document object using ResultSet. Currently i have been building document object in procedural way.
Now is there any Object oriented way to simplify the xml document building logic.so that it can accommodate the future changes also.
I have following structure of xml document,
<Response>
<BillingInformation>
<BillingID>
<Identifier>
</Identifier>
</BillingID>
<BillInfo>
<ID>
<Identifier>
</Identifier>
</ID>
<BillingCustomerName>
</BillingCustomerName>
<AlternateID>
<Identifier>
</Identifier>
<Type>
</Type>
</AlternateID>
<PostalAddress>
<ID>
<Identifier>
</Identifier>
<Type>
</Type>
</ID>
<StreetAddress>
<AddressContent>
</AddressContent>
<AddressContent>
</AddressContent>
</StreetAddress>
<City>
</City>
<State>
</State>
<Country>
</Country>
<Zip>
</Zip>
<Status>
</Status>
</PostalAddress>
<Contact>
<ContactId>
<Identifier>
</Identifier>
<Type>
</Type>
</ContactId>
<AlternateContactId>
<Identifier>
</Identifier>
<Type>
</Type>
</AlternateContactId>
<ContactName>
<FirstLastName>
<FirstName>
</FirstName>
<LastName>
</LastName>
</FirstLastName>
</ContactName>
</Contact>
<Status>
</Status>
</BillInfo>
</BillingInformation>
</Response>
Any help is appreciated.
I've used the WebRowSet Class to accomplish this in the past.
http://docs.oracle.com/javase/1.5.0/docs/api/javax/sql/rowset/WebRowSet.html
You might find it the easiest approach
Use JPA and JAXB. With JPA you will read (map) DB records into BillingInformation Java object(s), then marshal it to XML with JAXB.
XStream allow to map Objects to XML.
You have to create a Object hierarchy like your XML and anotate the classes with node names.
Works well with simple XML structures, as shown here: http://x-stream.github.io/alias-tutorial.html.

How to parse this xml file?

I am making a quiz game with content in a xml files. I used java language
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<LQuiz>
<Topic id="1" name="About Sun" time="900">
<question
name="1. The radiations emitted by the sun and responsible for the cause of skin cancer are...">
<answer correctness="true">ultra-violet</answer>
<answer correctness="false">infra-red</answer>
<answer correctness="false">X-rays</answer>
<answer correctness="false">micro waves</answer>
<advice>One of two first</advice>
</question>
<question name="2. The source of energy of the sun is...">
<answer correctness="false">nuclear fission</answer>
<answer correctness="false">chemical reaction</answer>
<answer correctness="true">nuclear fusion</answer>
<answer correctness="false">photoelectric effect</answer>
<advice>no prompt</advice>
</question>
</Topic>
<Topic id="2" name="Natural Resources" time="600">
<question name="1. What are some examples of Non-Renewable Resources?">
<answer correctness="true">Coal</answer>
<answer correctness="true">Oil</answer>
<answer correctness="false">Biomass</answer>
<advice>no prompt</advice>
</question>
<question name="2. What are some alternative sources of energy?">
<answer correctness="true">Wind</answer>
<answer correctness="true">Hydro</answer>
<answer correctness="false">Natural gas</answer>
<answer correctness="true">Fusion</answer>
<advice>Select correct variants</advice>
</question>
</Topic>
</LQuiz>
Can anybody help me how to parse this xml file using kxmls or ksoaps ? Sorry if this is a bad question. I need a example code please.
The wiki for the ksoap2-android project has a number of links that explain it all to you. If you have detailed questions ask another question here or on the project mailing list.

Categories