Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Moxy getValueByXpath gives null

Tags:

jaxb

xpath

moxy

I want to see if a theme element exists with specified name in the following xml file.

input.xml

<data>
    <artifacts>
        <document id="efqw4eads">
            <name>composite</name>
        </document>
        <theme id="1">
            <name>Terrace</name>
        </theme>
        <theme id="2">
            <name>Garage</name>
        </theme>
        <theme id="3">
            <name>Reception</name>
        </theme>
        <theme id="4">
            <name>Grade II</name>
        </theme>
    </artifacts>
</data>

I have the following code. return true statement of the method is never executed. answer always contains a null value.

public boolean themeExists(String name, Data data){
    String expression = "artifacts/theme[name='"+name+"']/name/text()";
    String answer = jaxbContext.getValueByXPath(data, expression, null, String.class);
    if(answer == null || answer.equals("")){
        return false;
    }
    return true;
}
like image 839
waqas Avatar asked Dec 06 '25 05:12

waqas


2 Answers

This use case isn't currently supported by EclipseLink JAXB (MOXy). I have opened the following enhancement you can use to track our progress:

  • http://bugs.eclipse.org/413823
like image 128
bdoughan Avatar answered Dec 09 '25 04:12

bdoughan


There is no <artifacts/> element you're look for in the first axis step. Your XPath expression should be something like

String expression = "data/theme[name='"+name+"']/name/text()";
like image 24
Jens Erat Avatar answered Dec 09 '25 03:12

Jens Erat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!