Results 1 to 2 of 2
  1. #1
    master_x is offline Member
    Join Date
    Jul 2010
    Location
    Atlanta, GA
    Posts
    1
    Rep Power
    0

    Question XPath using JDom question...

    In my code below, I am trying to isolate my 'handler' XML elements using XPath, but I am having no luck - the 'elemHandler' element is always null. Can anyone share with me the obvious solution? Thanks in advance.
    master_x.

    PS. The last line of code in the try-block can be ignored - was used for a breakpoint only.
    ================================================== =

    Java Code:
    import java.io.IOException;
    import java.io.StringReader;
    
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.xpath.XPath;
    
    public class XPathTest {
    
    	private static String jobString = "<dgs:job xmlns:dgs=\"http://service.anywhere.org/dgs\" name=\"Workflow.Certificate.JOB\"><handler name=\"xslt.transformation.handler\"/><handler name=\"openoffice.conversion.handler\"><renderOptions input=\"ODS\" output=\"PDF\"/></handler><handler name=\"workflow.attachment.handler\"><documentType>CERTIFICATE</documentType><documentName>Cert.pdf</documentName></handler><handler name=\"workflow.signal.handler\"><targetUri>http://service.anywhere.biz/cls</targetUri></handler></dgs:job>";
    
    	public static void main(String[] args) {
    		try {
    			Element elemJobInfo = new SAXBuilder().build(new StringReader(jobString)).detachRootElement();
    			XPath handlerExpression = XPath.newInstance("//stp:handler[2]");
    			handlerExpression.addNamespace("stp", elemJobInfo.getNamespaceURI());
    			Element elemHandler = (Element) handlerExpression.selectSingleNode(elemJobInfo);
    			jobString = elemHandler.toString();
    		}
    		catch (IOException e) {
    			System.out.println("Failure: " + e);
    		}
    		catch (JDOMException e) {
    			System.out.println("Failure: " + e);
    		}
    		catch (Exception e) {
    			System.out.println("Failure: " + e);
    		}
    	}
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 07-24-2010 at 01:32 PM. Reason: Code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I wonder why don't use inbuilt dom object to do this.

    Anyway since this is a third party library I'm not sure the exact way to instantiate your XML stream. Did they provide any information relevant to that?

Similar Threads

  1. jdom question , show xml
    By pipikrk in forum New To Java
    Replies: 11
    Last Post: 05-05-2010, 02:20 PM
  2. Replies: 4
    Last Post: 02-18-2010, 04:24 PM
  3. XML JDOM XPATH NumberFormatException
    By Jazzperson in forum Advanced Java
    Replies: 3
    Last Post: 03-08-2009, 08:15 PM
  4. JDOM in java applications
    By boy22 in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:38 PM
  5. xpath question
    By Ed in forum XML
    Replies: 1
    Last Post: 07-09-2007, 12:14 AM

Posting Permissions

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