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.
Related
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 :)
I am trying to read an XML and writing into another XML by changing some attribute values When processing XML by means of standard DOM, attribute order is changing.
Here I am trying to change the 'id' attribute value of 'staff' element.
input xml:
<company xyz="xyz" def="def">
<staff id="1" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
output xml:
<company def="def" xyz="xyz">
<staff abc="abc" def="def" id="2" xyz="xyz">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Expecting output xml:
<company xyz="xyz" def="def">
<staff id="2" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Could someone please help with the source code.
Waiting for the responses.
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
I am currently developing a system that uses the following format of XML files to for configuration purposes:
<?xml version="1.0" encoding="utf-8"?>
<Branch>
<Department>
<Door id="1" enabled="true"/>
<Door id="2" enabled="true"/>
</Department>
<Department>
<Door id="3" enabled="true"/>
<Door id="4" enabled="false"/>
</Department>
<Department>
....
</Branch>
Do you know how could I parse this XML file?
I have been looking for other answers but I could only get it to work till the Department node. It isn't interpreting the Door node level.
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.