Mike S.
Mike S.

Reputation: 63

XML Parse for specific element in java

Recently I tried to parse an XML File to do some testings on it.

I managed to read the XML File but I struggle to get to the element I want to read.

My XML File looks like this:

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

I am trying to reach the BlockElement1 and Extract data from sBlockElement(i) where i=1,2

My code:

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

The mistake is probably here :NodeList nListPoints = doc.getElementsByTagName("BlockElement1"); but I can't find a way to take the next Element that is called BlockElement1 from a NodeList . (I know that this takes the elemnts from the beggining).

The problem is that it returns EVERY sBlockElement1 and not the specific one for the BlockElement1

Thanks!

Upvotes: 0

Views: 573

Answers (1)

nlopez
nlopez

Reputation: 351

It seems that there aren't any namespaces in your xml so you should consider using XPath as It is (as least for me) so much easy to use! More info about XPath here

Here is an example that I think can be helpful:

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

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

Upvotes: 1

Related Questions