Solved

need to extract for iteration through all values contained in <genre>XXXXX</genre> elements in an XML file.

Posted on 2006-11-28
8
297 Views
Last Modified: 2013-11-23
I need to extract for validation against a controlled list of acceptable values all of the items contained in a <genre>xxxx</genre> xml file.  I could do this by writing my own string parser, but I'm guessing there is a cleaner, "best practice" kind of way to do this using JDOM, SAX or another xml parser.  Help appreciated.
0
Comment
Question by:wizcarl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 300 total points
ID: 18032290
you could use XPath for this. The string for this would be "//genre".

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
PathExpression expr = xpath.compile("//genre");
Object result = expr.evaluate(doc, XPathConstants.NODESET);

 NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
0
 

Author Comment

by:wizcarl
ID: 18033190
Sorry for the uneducated followup, but I just downloaded the org.apache.xalan jar file and it doesn't contain the XPath nor XPathFactory classess.  Do you know where I can get these jar files?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18034931
javax.xml.xpath.XPathFactory
javax.xml.xpath.XPath
javax.xml.xpath.XPathExpression (for PathExpression, sorry for the typo)
org.w3c.dom.NodeList

All of the above are included in the Java 1.5 SDK
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 18035135
0
 

Author Comment

by:wizcarl
ID: 18036315
Thank you.  I made these changes, and now the code is throuwing an java interal error at the line:

Object result = expr.evaluate(doc, XPathConstants.NODESET)

I think I'm feeding it the wrong kind of input object.  In my code, "doc" is  a string variable that contains all of the xml.  Any ideas of what it needs to be?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18036398
for a more simpler and complete example, check out http://www.vipan.com/htdocs/xmlHelp.html
You should supply a parsed document or xml element, not a string.
0
 

Author Comment

by:wizcarl
ID: 18036656
Just as a follow up, I tried changing the "doc" variable from a String to an Object and it thre the same error:

Exception in thread "main" java.lang.ClassCastException: java.lang.String
      at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:115)
      at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:97)
      at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:178)
      at org.nines.NinesCollexImporter.validateValues(NinesCollexImporter.java:576)
      at org.nines.NinesCollexImporter.main(NinesCollexImporter.java:116)
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 200 total points
ID: 18036677
The "doc" should be of Document type and not String, otherwise you cannot perform xpath queries on it. YO should turn the string into an XML document first before you proceed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 45
SequenceInputStream example 3 21
add projects t working set in maven 2 41
maven not picking latest jar instead picking old jar from .m2 12 46
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question