0

I've created an Axis2 web service client. The response being returned is:

<GetOpenPOsResponse xmlns="https://www.mycompany.net/">
<GetOpenPOsResult>
    <acXML xmlns="https://www.mycompany.net/acXMLSchema.xsd" lang="en-us">
        <Header>
            <From>
                <Company>Company Name</Company>
                <Identity>Id</Identity>
                <DatabaseName>Database</DatabaseName>
            </From>
        </Header>
        <Request/>
    </acXML>
</GetOpenPOsResult>

I'm able to select nodes via xpath if I use the following:

OMElement result = sender.sendReceive();
StAXOMBuilder builder = new StAXOMBuilder(result.getXMLStreamReader());
OMElement root = builder.getDocumentElement();

AXIOMXPath xpath = new AXIOMXPath("/*[name()='GetOpenPOsResponse']/*[name()='GetOpenPOsResult']/*[name()='acXML']/*[name()='Header']/*[name()='From']/*[name()='Company']");
OMElement selectedNode = (OMElement) xpath.selectSingleNode(root);
System.out.println(selectedNode.getText());

This returns: Company Name

But I want to simplify the xpath expression using namespaces as follows:

xpath.addNamespace("y", "https://www.mycompany.net");
xpath.addNamespace("x", "https://www.mycompany.net/acXMLSchema.xsd");

and change the xpath expression to:

 AXIOMXPath xpath = new AXIOMXPath("/y:GetOpenPOsResponse/y:GetOpenPOsResult/x:acXML/x:Header/x:From/x:Company");

But selecting the node this way returns null. I've seen other solutions where this approach should work but I can't seem to get it to work for me.
What am I doing wrong??

Thank You.

Andreas Veithen
  • 8,868
  • 3
  • 25
  • 28
rangelot
  • 1
  • 3

1 Answers1

0

This works perfectly- as it should - when I corrected the namespace for "https://www.mycompany.net". I forgot the "/" at the end.

xpath.addNamespace("y", "https://www.mycompany.net/");
rangelot
  • 1
  • 3