• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1084
  • Last Modified:

Java XML getNamedItem problem

I'm having problems getting an attribute from my XML.  I'm using Java 1.3

My string variable, pkgName is not being populated.

xml:
<host>
<pkg file="abc.txt">
...
</pkg>
</host>
NodeList children = currNode.getChildNodes();
    for(int y=0; y<children.getLength(); y++) {
            Node currChild = children.item(y);
             if(currChild.getNodeName().equals("host")) {
                 NodeList hostSubChildren = currChild.getChildNodes();
                 for(int i=0; i<hostSubChildren.getLength(); i++) {
                       Node currHostSubChild = hostSubChildren.item(i);
                       if(currHostSubChild.getNodeName().equals("pkg")) {
                           NodeList pkgNodes = currHostSubChild.getChildNodes();
                           for(int z=0; z<pkgNodes.getLength(); z++) {
                                    Node currPkgNode = pkgNodes.item(z);
                                    if(currPkgNode.getFirstChild() != null && currPkgNode.getAttributes().getNamedItem("file") != null )
                                         pkgName = currPkgNode.getAttributes().getNamedItem("file").getNodeValue().toString();

Open in new window

0
galaxy573
Asked:
galaxy573
  • 2
  • 2
1 Solution
 
CEHJCommented:
Go up the hierarchy until you find the first empty Node by using debug statements in the code
0
 
galaxy573Author Commented:
I was able to get my code working by using the code below.

The problem is there are four <pkg> tags in this particular XML file & the pkgName variable is a string, therefore only the last pkg file name is printing since the variable is re-assigned with each iteration.  

What's another way I can get around this problem?

I'm storing all the data from the various XML child node tags as Strings & then adding them to an arraylist of type Status Class. Status.java is my own class I created (with getters & setters).

I'm using a JSP to display the data.  I have an arraylist of type Status (class) which I loop through to get the data w/the getters.

Any ideas would be greatly appreciated!  I tried to put a bandaid over the problem w/the code below since it worked initially but ignored the fact that my boss would demo the application using a XML file with more than one <pkg> tag!  A case I forgot to account for!

If there's a fix for the syntax I originally posted that'd be great because then I can store the pkgName variable when I loop through the <host> tags; since <pkg> is a child of <host>

I believe the original code isn't working because I'm checking to see if the <pkg> tag exists, if it does, then I get all child nodes.  Then w/in that for loop get data from the child nodes.  The problem is, file is not a child node, it's an attribute.

if(currHostSubChild.getNodeName().equals("pkg")) {
                           NodeList pkgNodes = currHostSubChild.getChildNodes();
                           for(int z=0; z<pkgNodes.getLength(); z++) {
// ... code to get child Node data
}
}
// I need another if statment here to test if the file attribute exists, if so set it to pkgName.  What's the syntax for this or if there a better solution???
 NodeList pkgs = xmlDoc.getElementsByTagName("pkg");
    for(int c=0; c<pkgs.getLength(); c++) {
        Node pkgNode = pkgs.item(c);
        if(pkgNode.getFirstChild() != null && pkgNode.getAttributes().getNamedItem("file") != null)
           pkgName = pkgNode.getAttributes().getNamedItem("file").getNodeValue().toString();
                                      }

Open in new window

0
 
objectsCommented:
NodeList pkgs = xmlDoc.getElementsByTagName("pkg");
    for(int c=0; c
0
 
objectsCommented:
> pkgNode.getFirstChild() != null

not sure you need that check

0
 
galaxy573Author Commented:
I figured out a solution.

if(currHostSubChild.getNodeName().equals("pkg")) {
                           NodeList pkgNodes = currHostSubChild.getChildNodes();
                           for(int z=0; z<pkgNodes.getLength(); z++) {
// ... code to get child Node data
                            }
}
// If statement here to check if child node contains attributes

It wouldn't work if I checked w/in the if(currHostSubChild.getNodeName().equals("pkg")) { statement because none of the pkg child nodes have a attributes.  I had to go up one level to host and check it's child nodes.  In this case, the pkg node has an attribute so it worked.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now