Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts

    XQuery and XPath query to select only certain text depending on string

    Hi Guys and Girls,

    Looking for a bit of help with a Java Program i'm writing which receives a skill as a query and returns the employees who possess this skill as a result. For example show every who has the skill XML.

    JAVA
    Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package xmlskillquery;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
    public class XMLSkillQuery {
        public static void main(String[] args) {
     
            try {
                FileInputStream file = new FileInputStream(new File("C:\\Users\\Danny Stones\\Desktop\\Uni\\XML\\Practicle Part 1\\employees\\employees.xml"));
                     
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                 
                DocumentBuilder builder =  builderFactory.newDocumentBuilder();
                 
                Document xmlDocument = builder.parse(file);
     
                XPath xPath =  XPathFactory.newInstance().newXPath();
     
                System.out.println("*************************");
                String expression = "employees/employee/name[//employees/employee/skills/skill='XML']";
                NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
                for (int i = 0; i < nodeList.getLength(); i++) {
                    System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); 
                }
                System.out.println("*************************");
     
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }       
        }
    }
    XML Document
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet  type="text/xsl" href="employees.xsl"?>
    <employees>
      <employee id="em001">
        <name>Danny Stones</name>
    	<address>4 Keswick Close, Kirby Cross, Essex, CO13 0TG</address>
    	<phonenumber>01255 674223</phonenumber>
    	<photo>images/em001.gif</photo>
    	<skills>
    		<skill>XML</skill>
    		<skill>HTML</skill>
    		<skill>PHP</skill>
    	</skills>
      </employee>
      <employee id="em002">
        <name>Jane Murray</name>
    	<address>4 Spring Road, St Osyth, Clacton-on-Sea, Essex, CO16 8RN</address>
    	<phonenumber>01255 822192</phonenumber>
    	<photo>images/em002.gif</photo>
    	<skills>
    		<skill>JavaScript</skill>
    		<skill>HTML</skill>
    	</skills>
      </employee>
      <employee id="em003">
        <name>Ross Lilley</name>
    	<address>44 Carnarvon Road, Clacton-on-Sea, Essex, CO16 7GE</address>
    	<phonenumber>01255 222833</phonenumber>
    	<photo>images/em003.gif</photo>
    	<skills>
    		<skill>C++</skill>
    		<skill>HTML</skill>
    		<skill>PHP</skill>
    	</skills>
      </employee>
      <employee id="em004">
        <name>Eric Kong</name>
    	<address>67 Boadecia Way, Colchester, Essex, CO4 9SE</address>
    	<phonenumber>01206 877436</phonenumber>
    	<photo>images/em004.gif</photo>
    	<skills>
    		<skill>C++</skill>
    		<skill>XML</skill>
    		<skill>PHP</skill>
    	</skills>
      </employee>
      <employee id="em005">
        <name>Bradley Cadd</name>
    	<address>42 Eld Lane, Colchester, Essex, CO1 1LS</address>
    	<phonenumber>01206 239844</phonenumber>
    	<photo>images/em005.gif</photo>
    	<skills>
    		<skill>HTML</skill>
    	</skills>
      </employee>
      <employee id="em006">
        <name>Chris Stubbs</name>
    	<address>24 Grosvenor Square, London, W1K 6AH</address>
    	<phonenumber>020 8855 4689</phonenumber>
    	<photo>images/em006.gif</photo>
    	<skills>
    		<skill>JavaScript</skill>
    		<skill>C++</skill>
    	</skills>
      </employee>
    </employees>
    The JAVA program is working correctly its just getting the predicate write for the XPath statement.

    Any help would really be appreciated.

    Thanks in advance
    Danny

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,453
    Thanks
    1
    Thanked 21 Times in 19 Posts
    This line here might change your results:

    Code:
    <?xml-stylesheet  type="text/xsl" href="employees.xsl"?>
    Can you post that stylesheet? (I don't know if it will affect or not, but in a browser it would.)
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •