Are there any tools that can automatically generate java class hierarchy from xml (plist)?
Say we have:
<blah>
<item />
<item />
</blah>
And we need to get something like:
class Blah {
Collection<Item> items;
}
...and so on and so forth
If I get it right, then the elements shall be transformed into class and field names.
This can be done with a few lines of code:
parse the xml document into a DOM
walk through the DOM tree and create a java source file in memory (StringBuilder)
write the source file to your file system
Or use XSLT to create a transformation from your xml document to a java source file.
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.
In a XML file if i have two different root tags( i dont know what to call them ) like
in this example i have <units> and <extras>
<units>
<key_val
android:name="mega"
android:value="1000000" />
....
....
</units>
and
<extras>
<key_val
android:name="mega"
android:value="1000000" />
<key_val
android:name="kilo"
android:value="1000" />
.....
......
</extras>
in one xml file.Then how do i parse these different root tags in different Hashmaps. Like all the key-value pairs under <units></units> tag should go into one hashmap and for <extras></extras> in other hashmap.
XMLResourceParser will work for one kind of tag. So how do modify it to do for two?
This XML file does not consist of a Well-formed XML file. (See en.wikipedia.org/wiki/Well-formed_document)
One way to solve it: you can add a temporary root element to your XML file and parse it normally.
XML parsers will always read XML files line by line (DOM or SAX).
Another solution would be to separate your file into 2 XML files. Units.xml and Extras.xml!
I have the following data in my XML file.
<main>
<Team Name="Development" ID="10">
<Emp Source="Business" Total="130" Active="123" New="12" />
<Emp Source="Business" Total="131" Active="124" New="13" />
</Team>
<Team Name="Testing" ID="10">
<Emp Source="Business" Total="133" Active="125" New="14" />
</Team>
</main>
I want to read above data & store values into arrays,Can any one help on these?
Not sure why you need to convert those xml into Arrays, anyhow you can read xml and parse it by several ways. Normally we use DOM or Stax Parser and a Tutorial link is here, also here is a Java SAX Parsing Example tutorial.
Hope this can help you to achieve your goal. Update your question again if you stuck anywhere.
You can use parser in JAVA to parse the XML document. The package in java for this purpose is javax.xml.parsers . DocumentBuilder parses XML into a Document and Document is a tree structured data structure that is DOM(Document Object Model) readable file. Its nodes can be traversed/ changed/ accessed by DOM methods.
Here is a very good tutorial on XML DOM: http://www.roseindia.net/xml/dom/
and more specifically: http://www.roseindia.net/xml/dom/accessing-xml-file-java.shtml
also you can always refer to w3school for more theory on DOM!
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.
I'm trying to get informations from a XML file in Java with SAX.
I found some examples with a class that implements ContentHandler
interface and it work well when I run the parse method on an entire file
well formed with XmlReaderFactory class.
But my goal is to parse an XML file on the fly from stdin for example,
I'd like to get XML informations markups by markups like:
> <foo>
markup = foo
> <bar a="baz">
markup = bar
attribute a = baz
> </bar>
end markup bar
> </foo>
end markup foo
But when I pass theses inputs step by step to the parser it stops at the first entry
and say
[Fatal Error] :1:10: XML document structures must start and end within the same entity.
Is there a solution to do this.
I'm only allowed to use SAX to do this :-( for my school exercise.
Thanks for your help,
Arthur.
your markup is invalid xml. specifically the foo element is ended before the bar element
<foo>
<bar>
</foo>
</bar>
if the markup was correct, you should be able to do what you like.