简体   繁体   English

如何在不使用toString()的情况下从JAVA中的XmlObject提取信息?

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

I have an XmlObject (org.apache.xmlbeans.XmlObject) obj . 我有一个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? 在内容中存储“ n2”的正确方法是什么?

From the javadoc for SimpleValue - "All XmlObject implementations can be coerced to SimpleValue" Javadoc中获取SimpleValue- “可以将所有XmlObject实现强制为SimpleValue”

So the correct approach would be: 因此正确的方法是:

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

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. 在那里,您有了节点列表,可以从中获取所需的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM