1

Take an XML document like the following, but imagine it more complex with additional Nodes and Children for each Node.

<Vehicles> <FourWheels> <Truck> <HeavyDuty> <Engine></Engine> <BedSize length="6.5"/> <Exterior> <Doors> <Door> FrontLeft <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> <Door> FrontRight ///<------Door #2 <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> <Door> BackLeft <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> <Door> BackRight <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> </Doors> </Exterior> </HeavyDuty> <LightDuty> <Engine></Engine> <BedSize length="5.5"/> <Exterior> <Doors> <Door> FrontLeft <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> <Door> FrontRight ///<------Door #2 <DoorDetails> <InsidePanel> <Locktype type="B"/> ////<-----Check if type B </InsidePanel> </DoorDetails> </Door> <Door> BackLeft <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> <Door> BackRight <DoorDetails> <InsidePanel> <Locktype type="A"/> </InsidePanel> </DoorDetails> </Door> </Doors> </Exterior> <LightDuty> </Truck> <Car> : :Same as above. : </Car> </FourWheels> </Vehicles> 

Keep in mind there could be numerous other Nodes and Children involved.

If the objective is to check the LockType on the 2nd Door only, is it possible to jump right down to that particular Door and LockType to see if the attribute is "A" or "B"?

Or will you have to traverse through each parent/child/child/child.... relationship to get down to the particular Node and check the attribute?

EDITED TO ADD CODE BASED ON RECOMMENDED REFERENCE.

import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class VehicleTest { public static void main (String[] args) { VehicleTest test = new VehicleTest(); test.readXML(); } private void readXML() { Document doc = null; try { doc = parseXML("c:\\Downloads\\VehicleTest.xml"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(doc != null) { NodeList nList = doc.getElementsByTagName("Truck"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); System.out.println("nNode : " + nNode); Element eElement = (Element) nNode; System.out.println("eElement : " + eElement); Element cElement = (Element) eElement.getElementsByTagName("HeavyDuty").item(0); System.out.println("cElement : " + cElement); Element E2 = (Element) cElement.getElementsByTagName("Exterior").item(0); System.out.println("E2 : " + E2); Element E3 = (Element) E2.getElementsByTagName("Doors").item(0); System.out.println("E3 : " + E3); Element E4 = (Element) E3.getElementsByTagName("Door").item(1); System.out.println("E4 : " + E4); Element E5 = (Element) E4.getElementsByTagName("Locktype").item(0); System.out.println("E5 : " + E5); System.out.println("LockType : " + E5.getAttribute("type")); } } } private Document parseXML(String filePath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(filePath); doc.getDocumentElement().normalize(); return doc; } } 

Output :

enter image description here

I will have to play around with this. Although it works, it only processed one of the Truck items and need to process both.

In the code I provided, using "HeavyDuty" would case an exception for the 2nd iteration as its only LightDuty.

5
  • Can you refer this ? it might be of some help. For example : you can directly do doc.select("Doors") to get all Doors objects and if there are multiple you can select elements like you do in an array. It uses jsoup as library stackoverflow.com/questions/67693039/… Commented Jun 9, 2021 at 17:55
  • @Chandan, it a little more involved than this example because in my example, I would only want the Doors related to Trucks and not Cars. Using the above would bring back all references. Commented Jun 9, 2021 at 18:27
  • 1
    actually not very different. you have to do first select Truck and then Doors thats it like doc.select("Truck").select("Doors") Commented Jun 9, 2021 at 18:39
  • I just noticed you are referencing JSoup which I don't have and would not be able to install on the required machines. Commented Jun 9, 2021 at 19:06
  • I did take the code above in the Sample and applied it to my actual XML file and it worked nicely. Commented Jun 9, 2021 at 19:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.