XML解析Java中的特定元素

时间:2018-02-09 17:15:06

标签: java xml

最近我尝试解析XML文件以对其进行一些测试。

我设法阅读了XML文件,但我很难找到我想要阅读的元素。

我的XML文件如下所示:

<Information1>
 <Information1> Lot of elements here
 <Info2>
  <Info3> 
<BlockElement1> 
     <sBlockElement1> sElement1 </sBlockElement1>
     <sBlockElement2> sElements2 </sBlockElement2>
     <sBlockElement2>  sElements2 </sBlockElement2> !it has the same name but different values than the one above
</BlockElement1>
<BlockElement1> 
     <sBlockElement1> sElement1 </sBlockElement1>
     <sBlockElement2> sElements2 </sBlockElement2>
     <sBlockElement2>  sElements2 </sBlockElement2> !it has the same name but different values than the one above
</BlockElement1>
<BlockElement1> 
     <sBlockElement1> sElement1 </sBlockElement1>
     <sBlockElement2> sElements2 </sBlockElement2>
     <sBlockElement2>  sElements2 </sBlockElement2> !it has the same name but different values than the one above
</BlockElement1>
</Info3></Info2></Information1></Information1>

我正在尝试从sBlockElement(i)到达BlockElement1和Extract数据,其中i = 1,2

我的代码:

public static void main(String[] args) {
    try {
        Audiogramcurve aud=new Audiogramcurve();
        File fXmlFile = new File("C:\\Users\\UserNamer\\Desktop/My.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

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

        NodeList nList = doc.getElementsByTagName("Info3");
            for(int i=0;i<nList.getLength();i++) {
                Node nNode = nList.item(i);
                 System.out.println("\nCurrent Element :" + nNode.getNodeName());

                        NodeList nListPoints = doc.getElementsByTagName("BlockElement1");

                         for(int tT=0;tT<nListPoints.getLength();tT++) {
                                Node nNodePoint = nListPoints.item(tT);
                                System.out.println(nNodePoint.getNodeName());
                                NodeList audMesList=doc.getElementsByTagName("sBlockElement1");
                                NodeList tonPointList=doc.getElementsByTagName("sBlockElement2");
                                //System.out.println(tonPointList.getLength());
                                for(int audI=0;audI<tonPointList.getLength();audI++) {
                                    Node nNodeAud = tonPointList.item(audI);
                                    System.out.println(nNodeAud.getNodeName());                                 
                                }


                     }
                 }


    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} `

错误可能在这里:NodeList nListPoints = doc.getElementsByTagName("BlockElement1");但是我无法找到从NodeList获取名为BlockElement1的下一个Element的方法。 (我知道这需要从开始的元素)。

问题是它返回了每个sBlockElement1而不是BlockElement1的特定一个

谢谢!

1 个答案:

答案 0 :(得分:1)

似乎你的xml中没有任何命名空间,所以你应该考虑使用XPath,因为它是(至少对我来说)这么容易使用! More info about XPath here

以下是我认为有用的示例:

XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/Information1/Information1/Info2/Info3/BlockElement1/";

NodeList nodeList = (NodeList) xPath.evaluate(expression, document, XPathConstants.NODESET);