0

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!

Mike S.
  • 63
  • 4

1 Answers1

1

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);
nlopez
  • 351
  • 1
  • 13
  • its nice to use xpaths as long as you have no namespaces in xml. – Antoniossss Feb 09 '18 at 17:53
  • You are wright but it seems that, in this case, he has not namespaces. Thanks for the point! I edit my answer! – nlopez Feb 09 '18 at 18:26
  • I didnt say it is not the way to go - it was just a discaimer :) Using XPATH is still "easy" with namespaces, if you write some custom tweaks that are not supported out of the box nobody know why by JDK:P – Antoniossss Feb 09 '18 at 19:14
  • Thank you very much for your reply. I will try to use Xpath! – Mike S. Feb 10 '18 at 13:44
  • @MikeS. if you found helpful the answer, please check the green 'tick'! :) – nlopez Feb 10 '18 at 16:02