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
294 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
  • 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
 
LVL 35

Expert Comment

by:girionis
ID: 18035135
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now