I am trying to parse an xml that will define the position of the components in my swing application. I am new to DOM.
I do not know how to do this. can someone help me?
Here is my xml
<scenariotemplate>
<layout name="intro,quest,conc" width ="1" height="3">
<textElement name="Introduction" position="1"></textElement>
<listElement name="Questions" position="2"></listElement>
<textElement name="Conclusion" position="3"></textElement>
</layout>
</scenariotemplate>
here is my
java code. I am only tring to read the XML amd print the values here. I am geting a null point exception.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document
doc = docBuilder.parse (new File("scenario.xml"));
// normalize text representation doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList templates = doc.getElementsByTagName("layout");
int totaltemplates = templates.getLength();
System.out.println("Total no of templates : " + totaltemplates);
for(int s=0; s<templates.getLength() ; s++){
Node introNode = templates.item(s);
if(introNode.getNodeType() == Node.ELEMENT_NODE){
Element templateElement = (Element)introNode;
NodeList introlist = templateElement.getElementsByTagName("textElement");
Element introElement = (Element)introlist.item(0);
NodeList textFNList = introElement.getChildNodes();
System.out.println("introduction : " +
((Node)textFNList.item(0)).getNodeValue().trim());
NodeList questionList = templateElement.getElementsByTagName("listElement");
Element questionElement = (Element)questionList.item(0);
NodeList textLNList = questionElement.getChildNodes();
System.out.println("Question : " +
((Node)textLNList.item(0)).getNodeValue().trim());
NodeList conclusionList = templateElement.getElementsByTagName("textElement");
Element conclusionElement = (Element)conclusionList.item(0);
NodeList textAgeList = conclusionElement.getChildNodes();
System.out.println("Conclusion : " +
((Node)textAgeList.item(0)).getNodeValue().trim());