Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:33:57+00:00 2026-05-25T11:33:57+00:00

I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the

  • 0

I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the value of the specified element in the xml document refered to in ‘doc’. Assume doc at this point has been initialised as shown below and xmlInput is the buffer containing the xml content.

SAXBuilder  builder     =   null;
Document    doc     =   null; 
XPath       xpathInstance   =   null;

doc = builder.build(new StringReader(xmlInput));

When i call the method, i pass the following xpath xpression

/TOP4A/PERLODSUMDEC/TINPLD1/text()

Here is the method. It basically just takes an xml buffer and uses xpath to extract the value:

public static String getSingleNodeValue(String xpathExpr) throws Exception{

    Text list = null;

    try {
        xpathInstance = XPath.newInstance(xpathExpr);
        list = (Text) xpathInstance.selectSingleNode(doc);
    } catch (JDOMException e) {
        throw new Exception(e);
    }catch (Exception e){
        throw new Exception(e);
    } 

    return list==null ? "?" : list.getText();
}

The above method always returns “?” i.e. nothing is found so ‘list’ is null.
The xml document it looks at is

<TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">    
  <HEAD>
    <Doc>ABCDUK1234</Doc>  
  </HEAD>    
  <PERLODSUMDEC>
    <TINPLD1>10109000000000000</TINPLD1>
  </PERLODSUMDEC>
</TOP4A>

The same method works with other xml documents so i am not sure what is special about this one. There is no exception so the xml is valid xml. Its just that the method always sets ‘list’ to null. Any ideas?

Edit

Ok as suggested, here is a simple running program that demonstrates the above

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

import java.io.IOException;
import java.io.StringReader;

public class XpathTest {


    public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{

        Text list = null;
        SAXBuilder  builder         =   null;
        Document    doc             =   null; 
        XPath       xpathInstance   =   null;

        try {
            builder = new SAXBuilder(); 
            doc     = builder.build(new StringReader(xmlInput));

            xpathInstance = XPath.newInstance(xpathExpr);
            list = (Text) xpathInstance.selectSingleNode(doc);
        } catch (JDOMException e) {
            throw new Exception(e);
        }catch (Exception e){
            throw new Exception(e);
        } 

        return list==null ? "Nothing Found" : list.getText();
    }

    public static void main(String[] args){

        String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
        String xpathExpr = "/TOP4A/PERLODSUMDEC/TINPLD1/text()";

        XpathTest xp = new XpathTest();
        try {
            System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When i run the above, the output is

Nothing found

Edit

I have run some further testing and it appears that if i remove the namespace url it does work. Not sure why yet. Is there any way i can tell it to ignore the namespace?

Edit

Please also note that the above is implemented on JDK1.4.1 so i dont have the options for later version of the JDKs. This is the reason why i had to stick with Jdom.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:33:58+00:00Added an answer on May 25, 2026 at 11:33 am

    The problem is with XML namespaces: your XPath query starts by selecting a ‘TOP4A’ element in the default namespace. Your XML file, however, has a ‘TOP4A’ element in the ‘http://www.testurl.co.uk/enment/gqr/3232/1’ namespace instead.

    Is it an option to remove the xmlns from the XML?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined [AfterScenario] binding in my stepdefinition class and have method which will
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method in .NET (C#) which returns string[][] . When using RegAsm
I have a method which should be executed in an exclusive fashion. Basically, it's
I have method to which I pass an object. In this method I check
I have method which create background thread to make some action. In this background
I have method (which is part of IMyInteface) like this: interface IMyInterface { void
I'm trying to have method void run( string method ) which would run method
I have method which gets a POJO as it's parameter. Now I want to
I have method in Class which is implementation of Interface. When I made it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.