10 Replies Latest reply: Mar 15, 2007 6:46 AM by 807606 RSS

    parse xml document with xpath

    807606
      I would like to parse an xml document using xpath, see:
      http://www.onjava.com/pub/a/onjava/2005/01/12/xpath.html

      however, in the documentation (in the link above), it states that I need J2SE 5.0.
      Currently, I have:
      $ java -version
      java version "1.5.0_11"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
      Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

      Does that mean that I really have to install J2SE 5.0 in order to use J2SE 5.0's XPath support? Does anybody know anything about getting started with XPath, and whether I can just use my existing version of Java? I've never used XPath.
      For more documentation, one can view:
      http://www.w3.org/TR/xpath20/

      Thank you.
        • 1. Re: parse xml document with xpath
          807606
          1.5 == 5.0

          you've got it
          • 2. Re: parse xml document with xpath
            807606
            Check this out if you're confused: http://java.sun.com/j2se/1.5.0/docs/relnotes/version-5.0.html
            • 3. Re: parse xml document with xpath
              807606
              Thank you both very much, I get it. Trying to make sense of names of different java versions, libraries, products etc is actually a lot more confusing for novices than some of those permanently in the Java world may realise. Anyway thanks a lot.

              Incidentally, do you know how to add something to a class path variable?
              And whether XALAN-JAVA is automatically installed with 5.0?
              http://xml.apache.org/xalan-j/
              And whether JDOM is automatically installed with 5.0?
              If I need to install these first, I need to add a lot of .jar files to the classpath, which I have never done. Advice very much appreciated.
              p.s.I'm using eclipse.

              Message was edited by:
              TheMenkster
              • 4. Re: parse xml document with xpath
                807606
                Incidentally, do you know how to add something to a
                class path variable?
                You can, but the class loader won't notice the change. If you need to load classes dynamically, you will need to use your own URLClassLoader, or a loader of your own making.
                And whether XALAN-JAVA is automatically installed
                with 5.0?
                sun morphs a version of xalan into it's packages. If you use the factory methods that the TransformerFactory provides you shouldn't need to know the class names.
                http://xml.apache.org/xalan-j/
                And whether JDOM is automatically installed with
                5.0?
                JDOM is not installed. Sun has morphed a version of xerces dom parser into 5.0 however, like it did with xalan.
                If I need to install these first, I need to add a lot
                of .jar files to the classpath, which I have never
                done. Advice very much appreciated.
                p.s.I'm using eclipse.
                If you need 3rdparty jars, you can add them in eclipse by right clicking the project, choosing the 'Java Build Path' item, selecting the 'libraries' tab, and adding them there.
                • 5. xpath tutorial
                  807606
                  I have copied the code for the xpath tutorial on
                  http://www.onjava.com/pub/a/onjava/2005/01/12/xpath.html
                  exactly as it is on the tutorial, and imported the correct jars (I think).
                  But still my code is giving lots of errors!
                  Granted its my first shot but anyway here's the code and the errors...
                  package test;
                  
                  import org.jdom.xpath.*;
                  import java.io.*;
                  import javax.xml.xpath.*; 
                  
                  public class XPath {
                  
                       public static void main (String [] args){
                  
                            XPathFactory factory = XPathFactory.newInstance();
                            XPath xPath=factory.newXPath();
                            
                            XPathExpression  xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
                            File xmlDocument = new File("/home/myrmen/workspace/Testing/test/catalog.xml");     
                            //FileInputStream fis = new FileInputStream(xmldocument);  
                            InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
                            String title = xPathExpression.evaluate(inputSource);
                            String publisher = xPath.evaluate("/catalog/journal/@publisher", inputSource);
                            String expression="/catalog/journal/article";
                            NodeSet nodes = (NodeSet) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
                            NodeList nodeList=(NodeList)nodes;
                  
                  
                  
                            SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
                  
                            org.jdom.Document jdomDocument = saxBuilder.build(xmlDocument);
                  
                            org.jdom.Attribute levelNode = (org.jdom.Attribute)(XPath.selectSingleNode(jdomDocument,"/catalog//journal[@title='JavaTechnology']" + "//article[@date='January-2004']/@level"));
                            levelNode.setValue("Intermediate");
                            org.jdom.Element titleNode = (org.jdom.Element) XPath.selectSingleNode( jdomDocument,"/catalog//journal//article[@date='January-2004']/title");
                  
                            titleNode.setText("Service Oriented Architecture Frameworks");
                            java.util.List nodeList = XPath.selectNodes(jdomDocument,"/catalog//journal[@title='Java Technology']//article");
                            Iterator iter=nodeList.iterator();
                            while(iter.hasNext()) {
                                 org.jdom.Element element = (org.jdom.Element) iter.next();
                                 element.setAttribute("section", "Java Technology");
                            }
                  
                            XPath xpath = XPath.newInstance("/catalog//journal:journal//article/@journal:level");
                            xpath.addNamespace("journal", "http://www.w3.org/2001/XMLSchema-Instance");
                            levelNode = (org.jdom.Attribute) xpath.selectSingleNode(jdomDocument);
                            levelNode.setValue("Advanced");
                       }
                  
                  }
                  
                  
                  Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
                       Type mismatch: cannot convert from XPath to XPath
                       The method compile(String) is undefined for the type XPath
                       InputSource cannot be resolved to a type
                       InputSource cannot be resolved to a type
                       NodeSet cannot be resolved to a type
                       NodeSet cannot be resolved to a type
                       NodeList cannot be resolved to a type
                       NodeList cannot be resolved to a type
                       SAXBuilder cannot be resolved to a type
                       SAXBuilder cannot be resolved to a type
                       The method selectSingleNode(Document, String) is undefined for the type XPath
                       The method selectSingleNode(Document, String) is undefined for the type XPath
                       Duplicate local variable nodeList
                       The method selectNodes(Document, String) is undefined for the type XPath
                       Iterator cannot be resolved to a type
                       The method newInstance(String) is undefined for the type XPath
                       The method addNamespace(String, String) is undefined for the type XPath
                       The method selectSingleNode(Document) is undefined for the type XPath
                  
                       at test.XPath.main(XPath.java:12)
                  • 6. Re: xpath tutorial
                    807606
                    import org.jdom.xpath.*;
                    import java.io.*;
                    import javax.xml.xpath.*;

                    I'm guessing that jdom's xpath has classes the conflict with the javax ones. I would remove the jdom imports, myself and stick with straight transformer code.
                    • 7. Re: xpath tutorial
                      DrClap
                      I think I disagree; if you're using JDOM parsing and JDOM has an XPath implementation, then it would be better to use JDOM's XPath implementation. I think.
                      • 8. Re: xpath tutorial
                        807606
                        Well, who ever is right, you can decide, but just use one of them.
                        • 9. Re: xpath tutorial
                          807606
                          To parse an XML document with the JDK 5.0 XPath class,
                          add the <JDK5.0>\jre\lib\rt.jar file to the CLASSPATH
                          variable, if it's not already in the CLASSPATH.
                          Add rt.jar to the CLASSPATH? What's that guy smoking?

                          My advice is to find a different tutorial; this one is doing more harm than good.
                          • 10. Re: xpath tutorial
                            807606
                            Yes I know I threw in all of the code from both examples, JDOM and javax, but that's because when I originally tried just the code for the JDOM example on its own, that did not work. Here is the code for the JDOM example, and the errors:
                            package test;
                            
                            import org.jdom.xpath.*;
                            import java.io.*;
                            import java.util.*;
                            import org.jdom.*;
                            
                            public class XPath {
                            
                                 public static void main (String [] args){
                            
                                      File xmlDocument = new File("/home/myrmen/workspace/Testing/test/catalog.xml");     
                                      
                                      SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
                            
                                      org.jdom.Document jdomDocument = saxBuilder.build(xmlDocument);
                            
                                      org.jdom.Attribute levelNode = (org.jdom.Attribute)(XPath.selectSingleNode(jdomDocument,"/catalog//journal[@title='JavaTechnology']" + "//article[@date='January-2004']/@level"));
                                      levelNode.setValue("Intermediate");
                                      org.jdom.Element titleNode = (org.jdom.Element) XPath.selectSingleNode( jdomDocument,"/catalog//journal//article[@date='January-2004']/title");
                            
                                      titleNode.setText("Service Oriented Architecture Frameworks");
                                      java.util.List nodeList = XPath.selectNodes(jdomDocument,"/catalog//journal[@title='Java Technology']//article");
                                      Iterator iter=nodeList.iterator();
                                      while(iter.hasNext()) {
                                           org.jdom.Element element = (org.jdom.Element) iter.next();
                                           element.setAttribute("section", "Java Technology");
                                      }
                            
                                      XPath xpath = XPath.newInstance("/catalog//journal:journal//article/@journal:level");
                                      xpath.addNamespace("journal", "http://www.w3.org/2001/XMLSchema-Instance");
                                      levelNode = (org.jdom.Attribute) xpath.selectSingleNode(jdomDocument);
                                      levelNode.setValue("Advanced");
                                 }
                            
                            }
                            Exception in thread "main" java.lang.Error: Unresolved compilation problems:
                                 SAXBuilder cannot be resolved to a type
                                 SAXBuilder cannot be resolved to a type
                                 The method selectSingleNode(Document, String) is undefined for the type XPath
                                 The method selectSingleNode(Document, String) is undefined for the type XPath
                                 The method selectNodes(Document, String) is undefined for the type XPath
                                 The method newInstance(String) is undefined for the type XPath
                                 The method addNamespace(String, String) is undefined for the type XPath
                                 The method selectSingleNode(Document) is undefined for the type XPath

                                 at test.XPath.main(XPath.java:40)

                            I could try to find a new tutorial, but because I'm a newbie I don't know what tutorial would be bad or good, or relevant. This was the tutorial recommended to me by a more experienced Java user, I could look for another one. But in relation to the current code, now that I'm just using JDOM, any ideas why all these errors are going on?

                            Actually...I think there may be something amiss with classpaths. I will investigate. But if you have any tips to point me in the right direction, please post them! Thanks.

                            Message was edited by:
                            TheMenkster