如何在java中为xml解析创建POJO类?

时间:2012-06-15 10:00:16

标签: java xml-parsing pojo

我得到了我需要的确切输出,但我必须为我的程序使用POJO类,我搜索了很多问题,但我没有明确的想法。帮助我解决这个问题,提前谢谢,我的xml编码下面给出的java解析。

ReadAndPrintXMLFile ::

的编码
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.net.URL;
import java.io.InputStream;

public class ReadAndPrintXMLFile{
    public static void main (String argv []){


    try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

            URL url = new URL("http://xxxxxxxxxxxxxxxx");
            InputStream stream = url.openStream();
            Document doc = docBuilder.parse(stream);

            // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " +
                 doc.getDocumentElement().getNodeName());
            NodeList listOfPersons = doc.getElementsByTagName("head");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of head : " + totalPersons);
            for(int s=0; s<listOfPersons.getLength() ; s++){
                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
                    Element firstPersonElement = (Element)firstPersonNode;
                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("heading");
                    Element firstNameElement = (Element)firstNameList.item(0);
                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out.println("Heading : " +
                           ((Node)textFNList.item(0)).getNodeValue().trim());

                }//end of if clause
            }//end of for loop with s var
        }catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line "
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());
        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();
        }catch (Throwable t) {
        t.printStackTrace ();
        }
        //System.exit (0);
    }//end of main
}

对于这个xml解析程序,我必须使用POJO类,。为此,我创建了一个这样的类

public class POJOurl {

    private String heading;

    public String getHeading() {
        return heading;
    }

    public void setHeading(String heading) {
        this.heading = heading;
    }


}

这里我不知道如何在我的程序中使用这个get和set方法,使用这些公共String getHeading()和public void setHeading(String heading)我必须执行程序..我必须得到输出什么我现在正在接受..唯一的事情是我必须使用POJO课程这个程序

输出:

Root element of the doc is root1
Total no of head : 4
Heading : Appliance Repairs
Heading : Air conditioning and refrigeration services
Heading : Accountants
Heading : Accident Management

1 个答案:

答案 0 :(得分:0)

我会创建一个具有您需要的属性的Person,例如的firstName。