Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problems with XPathExpression on memory built Document

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with XPathExpression on memory built Document

    Hi,

    I'm working with XML documents that are built in memory. This seems to work jsut fine but I've got a problem while trying to evaluate an XPathExpression on the document. When I do it returns a null result. Here's an example of what I'm trying to do and maybe someone might have an idea how I can fix this issue:

    The XML looks similar to this:
    <mta>
         <subject>Some Text</subject>
         <!-- other stuff under here as well -->
    </mta>

    My code:

    Object results;
    NodeList ndlst;
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//mta/subject/text()");
    results = expr.evalutate(doc, XPathConstants.NODESET);
    ndlst = (NodeList) results;

    The NodeList should have one node in it but it if I were to request a .getLength() from it then it returns 0

    As I said the document is built in memory (not read from a flat file). I have tested this XPath expression on a flat file and it works. Only when the document is built in memory do I have this problem.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problems with XPathExpression on memory built Document

    This works on a flat file for me as well.

    Could you show us the code you use to build the document into memory?

    // Json

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with XPathExpression on memory built Document

    Actually I figured out the problem this morning. The problem was that I was adding the information with the wrong function. I had assumed that since I was getting the information with the getNodeValue() function I should use setNodeValue() to add the data.

    I found that I need to use the setTextContent() function to create the document.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problems with XPathExpression on memory built Document

    Aha, good job you sorted it out, myself I prefer to use the JDOM stuff along with the SAXBuilder to do XML stuff or sometimes I like the Apache Commons Configuration to load XML data.

    // Json

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with XPathExpression on memory built Document

    I'm new to Java coming from more from Microsoft .NET and using the msxml for handling DOM and XPath. So I haven't had the chance to really get to work much with the other XML parsers available with Java.

  6. #6
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default emm... strange.

    yoo.. love this thread

Similar Threads

  1. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 10:33 PM
  2. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 11:49 AM
  3. Java memory management
    By trueacumen in forum Java Theory & Questions
    Replies: 5
    Last Post: August 12th, 2009, 03:40 AM
  4. Replies: 10
    Last Post: June 22nd, 2009, 08:45 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 08:04 AM

Tags for this Thread