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
298 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
by zero exception 10 83
Bot application - advice 3 80
Github api to fetch the number of collaborators in a git repo 1 67
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:
Suggested Courses

734 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