I have an issue where I want to store the content of a node I am parsing using the XmlPullParser as a string for later parsing. However I can't see how to do this without iterating over the entire content and creating the string myself by creating the start tags etc.
I have read this question, however it is going about it in exactly the way I want to avoid (preferably).
Here is some example XML below.
<result mime="text/xml">
<array>
<item key="action">
<str>testcustom</str>
</item>
<item key="examplearray">
<array>
<item key="0">
<str>Item 0</str>
</item>
<item key="1">
<str>Item 1</str>
</item>
<item key="2">
<str>Item 2</str>
</item>
</array>
</item>
</array>
</result>
My Question
So in this example, I want to get the contents of the result node as an xml string. I don't want to parse it I just want to store it as String for parsing at a later date.
If anyone has any better ideas please let me know.
The PullParser is not giving u that string. In fact the parser is acting like a point on the current token. In order to have the full content of a node - u would need to parse it. Parsing is then of course changing the position of the parser.
The kxml implementation provides a DOM like implementation which has an Element class. This one is giving handy methods for parsing the current element out of a XmlPullParser and also has a toString implementation.
http://www.kxml.org
Related
It is easy to parse XML in which tags name are fixed. In XStream, we can simply use #XStreamAlias("tagname") annotation. But how to parse XML in which tag name is not fixed. Suppose I have following XML :
<result>
<result1>
<fixed1> ... </fixed1>
<fixed2> ... </fixed2>
</result1>
<result2>
<item>
<America>
<name> America </name>
<language> English </language>
</America>
</item>
<item>
<Spain>
<name> Spain </name>
<language> Spanish </language>
</Spain>
</item>
</result2>
</result>
Tag names America and Spain are not fixed and sometimes I may get other tag names like Germany, India, etc.
How to define pojo for tag result2 in such case? Is there a way to tell XStream to accept anything as alias name if tag name is not known before-hand?
if it is ok for you to get the tag from inside the tag itself (field 'name'), using Xpath, you can do:
//result2/*/name/text()
another option could be to use the whole element, like:
//result2/*
or also:
//result2/*/name()
Some technologies (specifically, data binding approaches) are optimized for handling XML whose structure is known at compile time. Others (like DOM and other DOM-like tree models - JDOM, XOM etc) are designed for handling XML whose structure is not known in advance. Use the tool for the job.
XSLT and XQuery try to blend both. In their schema-aware form, they can take advantage of static structure information when it is available. But more usually they are run in "untyped" mode, where there is no a-priori knowledge of element names or structure, and everything is handled as it comes. The XSLT rule-based processing paradigm is particularly well suited to "semi-structured" XML whose content is unpredictable or variable.
I need help in parsing one xml. Though I know xml parsing very well. but I am stuck with parsing of one particular xml which having it's inner tags changing dynamically. That xml I am getting in respoonse of some API. Sample xml is as follows:
<addon_brunch_data is_array="true">
<item>
<Monsoon_Magic_Lunch_Buffet__Adult_>
<currency_code>INR</currency_code>
<pid>575</pid>
</Monsoon_Magic_Lunch_Buffet__Adult_>
</item>
<item>
<Theme_Park_Lunch>
<currency_code>INR</currency_code>
<pid>583</pid>
</Theme_Park_Lunch>
</item>
<item>
<All_Restaurants_except_Imagica_capital>
<currency_code>INR</currency_code>
<pid>553</pid>
</All_Restaurants_except_Imagica_capital>
</item>
</addon_brunch_data>
In this example, I am receiving the fixed tag <addon_brunch_data is_array="true"> which is having an arraylist of <item>.
But the problem I am facing is,these inner tags of are not fixed :
<Monsoon_Magic_Lunch_Buffet__Adult_>
<Theme_Park_Lunch>
<All_Restaurants_except_Imagica_capital>
Sometimes I am getting the inner tags of as listed above, but sometimes the response changes as :
<addon_brunch_data is_array="true">
<item>
<Great_Indian_Food_Festival___Adult>
<currency_code>INR</currency_code>
<pid>575</pid>
</Great_Indian_Food_Festival___Adult>
</item>
<item>
<Great_Indian_Food_Festival___Child>
<currency_code>INR</currency_code>
<pid>583</pid>
</Great_Indian_Food_Festival___Child>
</item>
<item>
<Healthy_Meal_Pack>
<currency_code>INR</currency_code>
<pid>553</pid>
</Healthy_Meal_Pack>
</item>
</addon_brunch_data>
I need to write pojos for this, but I have not come earlier to parse such xml which is having inner tags changing dynamically like in above example I tried to give.
Guys Please help on this.
Im trying to merge & evaluate xml using xpath in Java.
I know its xpath 1.0, and 2.0 added if statements.
Im still trying to resolve the issue, hopefuly with your help.
so, 2 files:
1. data.xml
2. view.xml
view.xml contains attributes with xpath expressions, for example:
<item name="/names/name" .... />
I wish to read the entire view.xml as dom-tree, then select all the attributes with xpath expressions and evaluate them.
in this case, with a data.xml such as:
<names>
<name>Bon</name>
</names>
I expect my resulting dom-tree to be:
<item name="Bon" .... />
so far, that isnt too problematic.
The issue comes where I want to be more demanding, such as "if /names/name="Bon" return "name exists"
so that the result would be
<item name="name exists" .... />
What possible ways I have to do such a thing with Java xpath engine (or maybe something else?)
I have an xml with the following structure:
<Items>
<item>
<IntItem>
<value>1</value>
</IntItem>
</item>
<item>
<BoolItem>
<value>true</value>
</BoolItem>
</item>
<item>
<StrItem>
<value>word</value>
</StrItem>
</item>
</Items>
It is list of items, that can be different types (bool, int, string). Could you help me write java class with annotation for above xml structure?
Are you wanting to actually generate the 'java class' with an xml file? (That is what the title says)
If so, then you should look into some modeling frameworks that allow you to generate code from xml files (models). Acceleo and EMF are just a couple. But there are more.
BUT, if you are just wanting to populate fields in your class with an xml file... look into some xml parsers for android and parse the xml and just assign the values to the fields. SAX and DOM are two very popular parsers for Android.
<tableCategory>
<item app_name="APPNAME1i" desc="BDESC1" lang="1ODDD"/>
</tableCategory>
In the above xml tag, I want to add one more item as follows-
<item app_name="APPNAME2" desc="DESC2" lang="2ODDD"/>
So finally the XML tag will look like-
<tableCategory>
<item app_name="APPNAME1i" desc="BDESC1" lang="1ODDD"/>
<item app_name="APPNAME2" desc="DESC2" lang="2ODDD"/>
</tableCategory>
I am getting the tag -
Element paletteElement = (Element) doc.getElementsByTagName("tableCategory").item(0); //tag
But not getting the clue after that.
Assuming you are using javax.xml.parsers, element.appendChild() will do.
[edited]
In case, you are looking for how to create a new Element, below is an example snippet
Element newElem = doc.createElement("item");
newElem.setAttribute(...);
...
...
elem.addChild(newElem);
Look at the Element.addContent() method