Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Searching an XML file and using getNextSibling()

843834Apr 13 2005 — edited Apr 14 2005
I apologise if this question has been answered before, but I haven't been able to find a precise answer to my problem.

Here is a snippet of my XML file:
<organization...>
<title>
<item...>
<title>
<adlcp:prerequisites>
...


What I want to do is search this file based on item title (org->item->title). When I find the corresponding entry, I want to traverse back up the tree to retrieve an attribute of the <item> tag, the <title> belonging to the organisation, and I also want to record the sibling node <adlcp:prerequisites>.

I have no trouble obtaining traversing up the tree from the item <title>, but as soon as I try calling getFirstChild() or getNextSibling(), my method throws an exception.

Here is an excerpt of my (somewhat shoddy) code:


try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
docScorm = db.parse(strScorm);

Element root = docScorm.getDocumentElement();
NodeList children = root.getChildNodes();
int n = children.getLength();

String strTopicName = "";
int i = 0 ;
Node genericNode = null ;
Node topicNode = null ;

while(i < n)
{
NodeList topicList = docScorm.getElementsByTagName("title");
genericNode = topicList.item(i);
strTopicName = genericNode.getFirstChild().getNodeValue();

if (strTopicName.equals(strTopic))
{
topicNode = topicList.item(i) ;
}

i++ ;
}

//Save the information in the ScormInfo object.
siItem.setTitle(strTopicName) ;
//We have the topic name - get the other info.
if (topicNode != null)
{
Node itemNode = topicNode.getParentNode() ;
NamedNodeMap nnmItemAtt = itemNode.getAttributes() ;
Node itemIdentifierNode = nnmItemAtt.getNamedItem("identifier") ;
//Fill in the ScormInfo object details.
siItem.setIdentifier(itemIdentifierNode.getFirstChild().getNodeValue()) ;


//I'd given up getting the organisation title, and settled for the identifier attribute instead.
Node orgNode = itemNode.getParentNode() ;
NamedNodeMap nnmOrgAtt = orgNode.getAttributes() ;
Node orgTitleNode = nnmOrgAtt.getNamedItem("identifier") ;
siItem.setOrganisation(orgTitleNode.getFirstChild().getNodeValue()) ;

//Try to get the sibling.
Node preReqNode = topicNode.getNextSibling() ;

...

====================================

And this is the point where it all goes wrong. Can anyone shed some light on what I am doing wrong? Any help would be greatly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 12 2005
Added on Apr 13 2005
4 comments
357 views