Face an issue in parsing XML to extract data from a specific node. I referred to Link1 Link2 Link3. Please note, am able to parse & get the data for other nodes in the below xml file like id, order_id etc. But for the below line / node, unable to extract the info of segment_id & instrument_id:
<trade segment_id="NSE-F&O " instrument_id="NSE:INFRATEL17NOVFUT">
Not sure if the way the XML file is setup or the way I am trying to extract the data for that specific node is wrong. Hope the specific issue I face is clear.
XML File:
<contract_note version="0.1">
 <contracts>
  <contract>
   <id>CNT-17/18-5310750</id>
   <name>CONTRACT NOTE CUM BILL</name>
   <description>None</description>
   <timestamp>2017-11-01</timestamp>
   <trades>
      <trade segment_id="NSE-F&O " instrument_id="NSE:INFRATEL17NOVFUT">
      <id>37513030</id>
      <order_id>1300000000352370</order_id>
      <timestamp>09:20:48</timestamp>
      <description>None</description>
      <type>buy</type>
      <quantity>1700</quantity>
      <average_price>444.2</average_price>
      <value>755140.0</value>
      </trade>
   </trades>
  </contract>
 </contracts>
</contract_note>
Code:
try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile);
        NodeList cNoteList = doc.getElementsByTagName("contract");
        Node nNode = cNoteList.item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
        for (int j = 1; j <= eElement.getElementsByTagName("trade").getLength(); j++) {
// Check if data can be read for Node - 'id'    
System.out.println(eElement.getElementsByTagName("id").item(j).getTextContent();
// Check if data can be read for segment_id & instrument_id         
System.out.println("Scrip: " + eElement.getElementsByTagName("trade").item(0).getTextContent());
                }
                }catch (Exception e) {
                    e.printStackTrace();
                }
Edit: Corrected the xml file info provided above.
 
    