1
 <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 姓名:Kumar 地址:街道:城市:州:国家:邮政编码:公司 ID 公司:性别:

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

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()); 

}
4

1 回答 1

0

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

getAttrs(文档文档,字符串标记名,字符串属性)

getContent(文档文档,字符串标签名)

这是主要功能:

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;
    }

输出是:

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

祝你好运!

于 2014-02-03T21:19:44.970 回答