<item>
<RelatedPersons>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
<RelatedPerson>
<Name>xy</Name>
<Title>asd</Title>
<Address>abc</Address>
</RelatedPerson>
</RelatedPersons>
</item>
I d like to parse this data with a SAXParser. How can i do this?
I know the tutorials about SAX, and i can parsing any normal RSS, but i can't parsing this datas only.
Define your Problem: What you can probably do is create a Value Object(POJO) called Person which has the properties: name, title and address. You aim of parsing this XML would then be to create an ArrayList<Person> object. Defining a definite data structure helps you build logic around it.
Choose a Parser : You can then use a SAX Parser or an XML Pull Parser to browse through the tags: see this lin for a tutorial on DOM, SAX and XML Pull Parser in Android.
Data Population Logic: Then while Parsing, whenever you encounter a <RelatedPersons> tag, instantiate a new Person object. When you encounter the respective Properties tag, read the value and populate it in this object. When you encounter a closing </RelatedPersons> dump this Person Object in the ArrayList. Depending on the Parser you use, you will have to use appropriate methods to browse to the child node/nested nodes.(Refer the link for details)
By the time you are done parsing the last item node you will have all the values in your ArrayList.
Note that this is more of a theoretical answer; I hope it helps.
Related
How to read data from a XML file in a generic way.generic way means in the sense idf I change the XML file at a later time no impact will be there to the out put format.
It should read the whole content of the XML file perfectly in key value pair.
You should try with a SAX Parser and put the key/value pairs in a Map.
http://docs.oracle.com/javase/7/docs/api/org/xml/sax/helpers/DefaultHandler.html
Using this Handler you can simply parse it from start to end.
See also here for an example.
I have a Tree which has multiple children. I have to serialize the tree to a string and deserialize it back from a string to a Tree. I am able to serialize the Tree to a string. I am using the following format:
(-- Begin children for an Node
)-- End children for a Node
, -- Delimiter for children for a Node
Ex: 1(11(111(1111),112),12(121,122,123),13(131))
However I am not able to deserialize the above string to a Tree in Java. Please help. Some Example code would help.
I am open to serializing the String to another format too as long as I am able to deserialize it.
If you're open to other formats (as stated) and use XStream it'll likely serialise/deserialise to XML without any further work on your part. You can further customise the serialisation as required.
How about using Jackon and serialize to JSon?
I am new working in Java and XML DOM parser. I had a requirement like read the xml data and store it inform of column and rows type.
Example:sample.xml file
<staff>
<firstname>Swetha</firstname>
<lastname>EUnis</lastname>
<nickname>Swetha</nickname>
<salary>10000</salary>
</staff>
<staff>
<firstname>John</firstname>
<lastname>MAdiv</lastname>
<nickname>Jo</nickname>
<salary>200000</salary>
</staff>
i need to read this XML file and store it in the above format:
firstName,lastName,nickName,Salary
swetha,Eunis,swetha,10000
john,MAdiv,Jo,200000
Java Code:
NodeList nl= doc.getElementsByTagName("*");
for(int i=0;i< nl.getLength();i++)
{
Element section = (Element) nl.item(i);
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
{
title = title.getNextSibling();
if (title != null)
{
String first=title.getFirstChild().getNodeValue().trim();
if(first!=null)
{
title = title.getNextSibling();
}
System.out.print(first + ",");
} }
System.out.println("");
}//for
I did the above code, but i am not able to find the way to get the data in the above column and row format. Can any one please please kindly help me in solving my issue, i am looking into it from past many days
Since this looks like homework, I'm going to give you some hints:
The chances are that your lecturer has given you some lecture notes and/or examples on processing an XML DOM. Read them all again.
The getElementsByTagName method takes an element name as a parameter. "*" is not a valid element name, so the call won't return anything.
Your code needs to mirror the structure of the XML. The XML structure in this case consists of N staff elements, each of which contains elements named firstname, lastname, nickname and salary.
It is also possible that your lecturer expects you to use something like XSLT or an XML binding mechanism to simplify this. (Or maybe this was intended to be XMI rather than XML ... in which there are other ways to handle this ...)
I kept getElementsByTagName method parameter "*" because to read the data dynamically.
Well, it doesn't work!! The DOM getElementsByTagName method does NOT accept a pattern of any kind.
If you want to make your code generic, you can't use getElementsByTagName. You will need to walk the tree from the top, starting with the DOM's root node.
Can you please provide me with sample data.
No. Your lecturer would not approve of me giving you code to copy from. However, I will point out that there are lots of XML DOM tutorials on the web which should help you figure out what you need to do. The best thing is for you to do the work yourself. You will learn more that way ... and that is the whole point of your homework!
1. The DOM Parser will parse the entire XML file to create the DOM object.
2. You will always need to be aware of the the type of output and the structure of xml returned when a request is fired on a web-service.
3. And its Not the XML structure of a reply which is returned from the Webservice that will be dynamic, but the child elements values and attributes can be Dynamic.
4. You will need to handle this dynamic behavior with try/catch block...
For further details on DOM PARSER, see this site...
http://tutorials.jenkov.com/java-xml/dom.html
I have the following xml structure already available with the childnodes as title, desc, symp, diag, treat addinfo etc. I want to check that whether addinfo contains any thing and also append some string to it like "This is additional info". There are many disease title I need to check the addifo tag according to disease title-title tag.
<chapter>
<disease type="Name">
<title>Name</title>
<desc>--------------</desc>
<symp>--------------</symptoms>
<diag>-------------</diagnosis>
<treat>-------------</treatment>
<addinfo></addinfo>
</disease>
</chapter
>
I am using XPath query for searching the content of the tags according to the disease name.
Thanks
XPath can only retrieve information from your document, it cannot modify it.
XQuery can produce a modified copy of your document, but it's not particularly easy, because you need to explicitly copy all the parts that you don't want to change.
XSLT is a better bet for producing a modified copy of the document; or XQuery Update Facility if you prefer.
You can use an XPath like this to see if addinfo is empty or not: //addinfo[not(node())]
UPDATE:
Ok, now am totally lost as to what you want. It sounds like a really straightforward problem. You would create some sort of getText() method that given an XML node, gets the text associated with it (if any or returns null or empty string). Then you would do a traversal of all the diseases and for each disease, perform your logic: if( string.isNullOrEmpty(getText(disease, "addinfo")) ) { // do something } -- if you want to check whether addinfo is empty or add stuff to it etc..
So, I wish to parse an xml schema and list all the elements along with their annotation and type. I looked at some java possibilities - the closest was XSOM. It seems like driving a truck trailer to get some milk from the neighborhood store.
I looked at JAXB, but there's no parse and list all elements against schemata.
I don't want to validate- only want to list the elements/type/annotation.
Groovy's xmlsurper is a decent parser, but can't parse XSD. Anything you know in Java,Groovy (or python)?
thank you for your time.
The SAX parser is very simple.