Skip to Main Content

Java Programming

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!

Problems using java.xml.xpath - How to get values from DTMNodeList?

807603Feb 11 2008 — edited Feb 11 2008
Sorry for the waffle in the subject title, but I was unsure what to call it. I hope this thread is in the correct forum.
I'm having problems using xpath to parse some data from an XML file. I am able to create an expression which obtains the elements I wish to retreive, but I'm unsure on how to go about getting their values. So far I have..
public int getTerms(int PMID)
	{
		
		String uri = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id="
			        + PMID + "&retmode=xml";
		
		// Create an XPath object with the XPathFactory class
		XPathFactory factory = XPathFactory.newInstance();
		XPath xPath = factory.newXPath();
		
		// Define an InputSource for the XML article
		InputSource inputSource = new InputSource(uri);
		
		// Create the expression 
		String expression = "/PubmedArticleSet/PubmedArticle/MedlineCitation/MeshHeadingList/MeshHeading/DescriptorName";
		// Use this expression to obtain a NodeSet of all the MeSH Headings for the article
		try
		{
			DTMNodeList nodes = (DTMNodeList) xPath.evaluate(expression,
	                inputSource, XPathConstants.NODESET);

			int length = nodes.getLength();
			String[] terms = new String[length];
			// Test mesh terms are being stored.
			 *for (int i=0; i<length; i++)*
*				System.out.println(i + ": " + nodes.item(i).getNodeValue());* 
                                                return nodes.getLength();
		}
		catch (XPathExpressionException e2)
		{ System.out.println("Article with PMID " + PMID + " has no associated MeSH terms");}
        
		// return a default
		return 0;
		
	} 
The part in bold is the problematic code. I wish to retreive the values of each of the nodes I have taken into my DTMNodeList using a for loop to iterate through each node, and use the getNodeValue() method from the Node class which should return a string. However, the values I retreive are NULL. In the test document I use, the elements do have values.

Here is a snippet of the XML file I am reading in:
<MeshHeadingList>
  <MeshHeading>
  <DescriptorName MajorTopicYN="N">Binding Sites</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Chromatium</DescriptorName> 
  <QualifierName MajorTopicYN="Y">enzymology</QualifierName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="Y">Cytochrome c Group</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Electron Spin Resonance Spectroscopy</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Flavins</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Heme</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Hydrogen-Ion Concentration</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Iron</DescriptorName> 
  <QualifierName MajorTopicYN="N">analysis</QualifierName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Magnetics</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Oxidation-Reduction</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Protein Binding</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Protein Conformation</DescriptorName> 
  </MeshHeading>
- <MeshHeading>
  <DescriptorName MajorTopicYN="N">Temperature</DescriptorName> 
  </MeshHeading>
  </MeshHeadingList>
Any help would be appreciated.. thanks :-)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 10 2008
Added on Feb 11 2008
4 comments
865 views