使用dom解析java中的xml

时间:2013-05-02 10:23:57

标签: xml dom

 <persons>  
    <people id="1">
       <name>Kumar</name>

       <address>
        <street doorNumber="1a">First Cross Street</street>

        <city>Chennai</city>

        <state>TamilNadu</state>

        <country>India</country>

        <postcode>600001</postcode>

      </address>

      <Company id="C101">TCS</Company>

    <sex>Male</sex>
  </people>
</persons>    

我想用dom以这种方式获得输出。 人物ID:1 名称:库马尔 地址: 街: 市: 州: 国家: 邮编: 公司编号 公司: 性:

我无法访问街道,城市,即所有子节点及其属性。 这是我的代码。

NodeList nList = doc.getElementsByTagName("people");


        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {                  
                Element eElement = (Element) nNode;

                System.out.println("People ID:" +eElement.getAttribute("id"));
        System.out.println("Name:" +eElement.getElementsByTagName("name").item(0).getTextContent());

        System.out.println("Address:" +eElement.getElementsByTagName("address").item(0).getTextContent());  

        System.out.print("Sex:" +eElement.getElementsByTagName("sex").item(0).getTextContent()); 

}

1 个答案:

答案 0 :(得分:0)

一般来说,解析xml,属性和内容需要两件事。 所以添加两个方法:

  

getAttrs(Document doc,String tagName,String Attr)

     

getContent(Document doc,String tagName)

这是主要功能:

public static void main(String args[]){

        try{
            File xmlFile = new File("C:/Users/syang/Desktop/Workspace/Test Station/ParseXMl/rec/testing1.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document xmlDom = dBuilder.parse(xmlFile);

            //normalization
            xmlDom.getDocumentElement().normalize();

            System.out.println("Root element: " + xmlDom.getDocumentElement().getNodeName());

            String id = getAttrs(xmlDom,"people","id");
            String name = getContent(xmlDom,"name");
            String doorNumber = getAttrs(xmlDom,"street","doorNumber");
            String streetName = getContent(xmlDom,"Street");
            String city = getContent(xmlDom,"city");
            String state = getContent(xmlDom,"state");
            String country = getContent(xmlDom,"country");
            String postcode = getContent(xmlDom,"postcode");
            String companyId = getAttrs(xmlDom,"Company","id");
            String companyName = getContent(xmlDom,"Company");
            String sex = getContent(xmlDom,"sex");

            System.out.println("People ID: " + id + "; Name: " + name + "; Address: " + doorNumber +
                    " " + streetName + ", " + city + ", " + state + ", " + country + "; Postcode: " + postcode +
                    "; Company ID: " + companyId + "; Company Name: " + companyName + "; People Sex: " + sex);


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static String getAttrs(Document doc, String tagName, String Attr){
        String result = "";

        NodeList nList = doc.getElementsByTagName(tagName);
        if(nList.getLength()>0){
            Element eElement = (Element)nList.item(0);
            String AttrStr = eElement.getAttribute(Attr);
            if(!"".equals(AttrStr )){
                result = String.valueOf(AttrStr);
            }

        }
        return result;
    }

    private static String getContent(Document doc, String tagName){
        String result = "";

        NodeList nList = doc.getElementsByTagName(tagName);
        if(nList.getLength()>0){
            Element eElement = (Element)nList.item(0);
            String contentStr = eElement.getTextContent();
            if(!"".equals(contentStr)){
                result = String.valueOf(contentStr);
            }

        }
        return result;
    }

输出结果为:

  

根元素:人物ID:1;姓名:库马尔;地址:1a,   印度泰米尔纳德邦钦奈;邮政编码:600001;公司编号:C101;公司   名称:TCS;人性别:男性

祝你好运!