• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

reading multple nested variables from XML

 
Ranch Hand
Posts: 176
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML document that have multiple levels of nesting:

I need to return all the different values within each child element... ie.
Virtue: Innocence
Rank: 1
Physical Mitigation = 84
Physical Rating = 89
Tactical Mitigation = 29
Virtue: Innocence
Rank: 2
Physical Mitigation = 162
Physical Rating = 175
Tactical Mitigation = 54

I can read the XML okay but I get an error on displaying the information.



Any suggestions?
 
lowercase baba
Posts: 12751
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Morgan wrote:Any suggestions?


Sure.

Tell us what exactly the error says.
 
John Morgan
Ranch Hand
Posts: 176
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

java.lang.NullPointerException
at MyJavaCalc.ReadXMLFile.main(ReadXMLFile.java:47)

Line 47 is "System.out.println("Rank : " + eElement.getFirstChild().getAttributes().getNamedItem("ranklvl").getTextContent());"
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess would be that "eElement.getFirstChild()" is not what you think it is - it's probably a text node containing nothing but white space.

If the structure is always like you posted it, an approach using XPath might be easier. DOM is a pain in the neck to work with, IMO.

(As an aside, the first step to debugging this should have been to determine -and post- which part is null - eElement? getFirstChild() ? getAttributes() ? or getNamedItem("ranklvl")?)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!