3

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?

VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
M.C.
  • 1,765
  • 3
  • 29
  • 31

2 Answers2

8

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");
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • why did you set the value "n2" . I would like to get that value . For me the first answer without the edit was correct – M.C. Apr 11 '13 at 14:58
  • Sorry, I thought I gave the wrong answer as you say "What is the right way to store" - I thought I had misread the question. In any case - have added both in there. – Boris the Spider Apr 11 '13 at 15:00
3

Something like this?

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("input.xml"));
    NodeList nodeList = document.getElementsByTagName("Xml-Fragment");

And there you have your nodelist, to take whatever you want from.

aran
  • 10,978
  • 5
  • 39
  • 69