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