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 namedfirstname
,lastname
,nickname
andsalary
.
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...
来源:https://stackoverflow.com/questions/11666140/how-to-modify-xml-data-in-dom-parser