• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difficulty Parsing XML Child Nodes

 
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My code follows my signature.

My XML is parsing correctly except the dependant names are blank.

I know the number of dependants are correct in each instance, but haven't been able to get the names parsed.

Please help with code. I've spun my wheels for days on this, and I have made progress but now I'm stuck here.

See DomParserExample.getEmployee(Element empEl) line 82 for a comment where I think the problem is.

EmployeeList.java is the main class.

Thanks,

Lou

Employees.xml


DomParserExample.java


Employee.java


EmployeeList.java
 
Sheriff
Posts: 28329
96
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

Lou Pellegrini wrote:See DomParserExample.getEmployee(Element empEl) line 82 for a comment where I think the problem is.



Yes, I agree. It looks like once you find a "name" element you call a method which looks for a child element called "name" and then extracts the text from it if there is one. But your "name" elements never have such children.
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Yes, I agree. It looks like once you find a "name" element you call a method which looks for a child element called "name" and then extracts the text from it if there is one. But your "name" elements never have such children.



Are you confusing the Employee Name element with the dependant name element?

After I get the Employee Name and other elements of the Employee there may be zero or 1 or many name elements in the dependant node I need to get.

Snippet from Employees.xml


I would greatly appreciate it if someone could show me the code that works.

Thanks,

Lou
 
Paul Clapham
Sheriff
Posts: 28329
96
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

Lou Pellegrini wrote:Are you confusing the Employee Name element with the dependant name element?



No, I'm not doing that. After all they have different names (XML is case-sensitive). After this line of code:



where you get the "name" elements which are the children of a dependent, you proceed to pass each of those elements to your getDependent() method which attempts to get all their descendants called "name".

Naturally there aren't any, so the getTextValue() method returns its default value of an empty string. Which is what you are seeing, no?
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Success!

My code change is posted after my signature.

No, I'm not doing that.



I didn't think so, just checking.

Naturally there aren't any, so the getTextValue() method returns its default value of an empty string. Which is what you are seeing, no?



Which is what I was seeing, yes!

As you have probably guessed I am new to XML.

Obtained the dependant values I was looking for.
Then just for grins I added

which returned empty Strings.

That causes me some confusion because I figured all <name> values would be individually in dependantName first child and siblings.

Now it looks like all <name> values would be in the in only the first child, I guess because they have the same tag name?
So would any other dependant values like <age> be in dependantName.getNextSibling().getNodeValue();?

Anything you can do to clairify this for me would be greatly appreciated!

Thanks,

Lou

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dependantName.getFirstChild().getNodeValue();



I really seriously distrust any XML parsing code that does a getFirstChild() without checking to see what kind of node it gets. So many ways to format XML that look right to you but have formatting Nodes of type Text where you don't expect them.

I recommend you spend considerable time with the lovely table in the JavaDocs for the org.w3c.dom package, Node class - this shows what the different types of Node return for nodeName and nodeValue - frequently not what you are expecting.

Bill
 
Paul Clapham
Sheriff
Posts: 28329
96
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

Lou Pellegrini wrote:Now it looks like all <name> values would be in the in only the first child, I guess because they have the same tag name?
So would any other dependant values like <age> be in dependantName.getNextSibling().getNodeValue();?



There aren't any <age> elements anywhere in the XML you posted. There are <Age> elements, but they aren't children of the <dependant> elements so they aren't "dependant values". In any reasonable understanding of the XML, they represent the Age of the Employee and not the Age of any dependant. Likewise there are <Name> elements which are children of <Employee> and <name> elements which are children of <dependant>. Don't get them confused.

And I would reiterate what Bill said -- it's extremely common for DOM beginners to not know that their documents are full of whitespace text nodes.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic