Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Can't retrieve ID's

  1. #1
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Can't retrieve ID's

    If my xml file (shown below as sample) has different employees with id's - is there a way I can get just the id's on there own?
    Java Code:
    <Employees>  
        <Employee id="1">  
            <age>29</age>  
            <name>Pankaj</name>  
            <gender>Male</gender>  
            <role>Java Developer</role>  
        </Employee>  
        <Employee id="2">  
            <age>35</age>  
            <name>Lisa</name>  
            <gender>Female</gender>  
            <role>CEO</role>  
        </Employee>  
    </Employees>
    This is the code Im using - I know at the minute it gets the id details of id 3 but realistically I just want the id's and nothing else
    Java Code:
     private static String getEmployeeById(Document doc, XPath xpath, int id) {  
            String env = null;  
            try {  
                XPathExpression pathExpression =  
                        xpath.compile("/Employees/Employee[@id=3]"); // line gets everything to do with id 3 but doesnt display the id - I need it to display all the id's  
                env = (String) pathExpression.evaluate(doc, XPathConstants.STRING);  
            } catch (XPathExpressionException e) {  
                e.printStackTrace();  
            }  
      
            return env;  
        }
    Can this be achieved using xpath? I've been stuck on it for ages and I can't seem to really find anything to do with it

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Can't retrieve ID's

    This should do it: //Employee/@id

    Edit: you can test it quickly here: Simple online XPath tester
    Fluffy93 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Can't retrieve ID's

    Thank you very much SurfMan,that works like a charm - thanks for the link too! It's very helpful, saving it! :D

Similar Threads

  1. Replies: 0
    Last Post: 01-09-2014, 06:11 PM
  2. Retrieve from DB and display in JSP
    By com.deepaksingh in forum Advanced Java
    Replies: 2
    Last Post: 08-08-2013, 07:47 PM
  3. retrieve output
    By manou2324 in forum New To Java
    Replies: 2
    Last Post: 04-30-2013, 01:22 PM
  4. retrieve a value
    By manou2324 in forum AWT / Swing
    Replies: 5
    Last Post: 03-29-2013, 05:11 PM
  5. Retrieve file
    By mine0926 in forum NetBeans
    Replies: 2
    Last Post: 06-03-2011, 04:47 AM

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
  •