• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difficulty Parsing XML Child Nodes

 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic