Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

java - How to modify XML data in Dom parser

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...