• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

parsing XML in java

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first you get a list of all the <textElement> elements. There are two of them. Then you get the first entry from that list, which is the first <textElement> element. Then you try to get its first child; but it doesn't have any children, because it's an empty element. So that returns null. And then you try to call null's trim() method, and that's where the exception occurs.

And later you repeat that process. You seem to think that the list of <textElements> is going to be a different list the second time you ask for it, but it isn't. My guess is that you're expecting to see the <textElement> element that has name="Introduction" the first time, and the element that has name="Conclusion" the second time. But the list contains both of those elements both times you ask for it. You won't get an exception here, just results different than what you expected.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you thought about using some tools like XStream or Castor that convert XMLs to POJOs?
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what exactly is your requirement?

Are you looking for an output like this?

Introduction : 1
Questions : 2
Conclusion : 3
 
today's feeble attempt to support the empire
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic