2013-12-20 130 views
0

我真的希望你能帮助我解决这个问题...... 我需要从我的xml文件中获取特定的数据,但我被困在一点,我无法弄清楚继续...Java XML解析复杂的xml文件

我想从网络:网络名称;来自代码:mcc和mnc代码;从设置:名称,ID,类型,参数名称和值;

所以这是我的XML文件的结构:

<country country="Andorra" isoCode="AD"> 
    <networks> 
     <network name="Mobiland" isMNO="true" ranking="10"> 
      <codes> 
       <code mcc="213" mnc="03" /> 
      </codes> 
      <settings> 
       <setting alternativeName="Mobiland AD" ref="s1" name="IAP" id="2266" /> 
       <setting alternativeName="Mobiland MMS AD" ref="s2" name="MMS" id="2265" /> 
      </settings> 
     </network> 
    </networks> 
    <settings> 
     <setting id="s2" type="mmssetting"> 
      <parameter name="mms-gprs-access-point-name" value="MMS" /> 
      <parameter name="mms-gprs-name" value="MMS" /> 
      <parameter name="mms-gprs-proxy" value="192.168.021.050" /> 
      <parameter name="mms-gprs-proxy-port" value="9201" /> 
      <parameter name="mms-url" value="http://mms.ad/mmsc" /> 
     </setting> 
     <setting id="s1" type="iapsetting"> 
      <parameter name="iap-gprs-access-point-name" value="internet" /> 
      <parameter name="iap-gprs-name" value="Internet" /> 
      <parameter name="iap-gprs-url" value="http://google.com" /> 
     </setting> 
    </settings> 
</country> 

这是我迄今为止......,我真的无法继续进行任何...我有一个空指针异常在String content = cNode.getLastChild()。getTextContent()。trim();而我不知道,我这样做的正确的方式.....

公共无效ReadXML的()抛出的ParserConfigurationException,SAXException中, IOException异常{

// Get the DOM Builder Factory 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

    // Get the DOM Builder 
    DocumentBuilder builder = factory.newDocumentBuilder(); 

    // Load and Parse the XML document 
    // document contains the complete XML as a Tree. 
    Document document = builder.parse("D:\\test.xml"); 

    List<Apn> empList = new ArrayList<Apn>(); 

    // Iterating through the nodes and extracting the data. 
    NodeList nodeList = document.getDocumentElement().getChildNodes(); 

    for (int i = 0; i < nodeList.getLength(); i++) { 

     Node node = nodeList.item(i); 
     if (node instanceof Element) { 
      //Apn apn = new Apn(); 
      System.out.println(node.getAttributes().getNamedItem("country") 
        .getNodeValue()); 

      NodeList childNodes = node.getChildNodes(); 
      for (int j = 0; j < childNodes.getLength(); j++) { 
       Node cNode = childNodes.item(j); 


       if (cNode instanceof Element) { 
        String content = cNode.getLastChild().getTextContent() 
          .trim(); 

        switch (cNode.getNodeName()) { 
        case "networks": 
         System.out.println(content); 
         break; 
        case "setting": 
         System.out.println(content); 
         break; 
        case "codes": 
         System.out.println(content); 
         // emp.location = content; 
         break; 
        } 
       } 
      } 

     } 

    } 
} 

任何帮助将appericiated!

谢谢!

+0

也许蓖麻项目(一OXM工具)可能是有用的你的任务:http://castor.codehaus.org/ –

+0

你可以编写一个xsd并在java对象中转换你的xml吗? –

+0

使用trang从xml生成Schema文件。 http://peter-on-java.blogspot.in/2012/10/create-xsd-from-xml-using-trang.html一旦你有模式文件,你可以使用XMl-java映射框架,如jaxb或castor。一旦你生成了java类,你就可以使用这些java类来将你的xml编译/解组成Java对象,反之亦然。 – thiyaga

回答

1

我认为对付这样的配合物的XML结构最简单的方法是写一个xsd schema,并通过使用JAXB

2

使用XPath转换XML转换为Java对象。例如,为了获得network元素的name属性的值,你可以这样做:

InputStream is = this.getClass().getResourceAsStream("country.xml"); 
InputSource inputSource = new InputSource(is); 

XPath xpath = XPathFactory.newInstance().newXPath(); 

String expression = "/country/networks/network/@name"; 
NodeList names = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); 
for (int i = 0; i < names.getLength(); i++) { 
    System.out.println(names.item(i).getNodeValue()); 
} 

输出:

Mobiland