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
296 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

808 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