How to extract information from XmlObject in JAVA without using toString()?

M.C. picture M.C. · Apr 11, 2013 · Viewed 7.4k times · Source

I have an XmlObject (org.apache.xmlbeans.XmlObject) obj .

    XmlObject obj;
    ...
    obj.toString(); //<xml-fragment>n2</xml-fragement>
    // content ="n2"
    String content = obj.toString().substring(14, obj.length() - 15) 

What is the right way to store "n2" in content?

Answer

Boris the Spider picture Boris the Spider · Apr 11, 2013

From the javadoc for SimpleValue - "All XmlObject implementations can be coerced to SimpleValue"

So the correct approach would be:

//to get the string value
((SimpleValue)obj).getStringValue();
//to set the string value
((SimpleValue)obj).setStringValue("n2");