This is the XML file I'm working on:
<book>
<chapter index="1" name="Chapter 2">
<verse index="1" text="First Line" />
<verse index="2" text="Second Line" />
<verse index="3" text="Third Line" />
</chapter>
<chapter index="1" name="Chapter 2">
<verse index="1" text="First Line" />
<verse index="2" text="Second Line" />
<verse index="3" text="Third Line" />
</chapter>
</book>
This is what I desperately tried, following an answer posted: How can I read Xml attributes using Java?
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Main
{
public static void main (String[] args)
{
try
{
File fXmlFile = new File("book.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Verse: " +
doc.getDocumentElement().getElementsByTagName("chapter").item(0).getChildNodes().item(0).getAttributes().getNamedItem("text").getNodeValue());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}