Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串解析为文档_Java_Android_Xml - Fatal编程技术网

Java 将字符串解析为文档

Java 将字符串解析为文档,java,android,xml,Java,Android,Xml,我正在尝试将字符串解析为文档。字符串的内容是我从SIP消息中获取的XML数据 我尝试这样做: DocumentBuilder db; Document pidf; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); db = factory.newDocumentBuilder(); InputSource is = new InputSource( new StringReader( content )

我正在尝试将字符串解析为文档。字符串的内容是我从SIP消息中获取的XML数据

我尝试这样做:

DocumentBuilder db;
Document pidf;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
db = factory.newDocumentBuilder();
InputSource is = new InputSource( new StringReader( content ));

pidf = db.parse(is);
但我得到了这个错误:

org.xml.sax.SAXParseException: Unexpected token (position:TEXT [B@fd41b3b@1:11 in java.io.StringReader@83dc258) 
我使用Log.d()查看字符串的内容,可以看到:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><presence xmlns="urn:ietf:params:xml:ns:pidf" xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:iot="urn:uma-etsit-ic:internet-of-things" xmlns:ts="PIDF" entity="pres:mymail@uma.es:5050"><tuple id="qoica32"><status><basic>open</basic></status><ts:timed-status from="2018-09-07T19:30:30.119+02:00" until="2018-09-07T20:30:30.129+02:00"><basic>close</basic></ts:timed-status></tuple><dm:device id="dtemp1"><iot:tecnologia><iot:802-11/></iot:tecnologia><iot:bateria>100</iot:bateria></dm:device><dm:person><iot:sensor><iot:temperatura/></iot:sensor><iot:unindad><iot:celsius/></iot:unindad><iot:valor>29.3</iot:valor></dm:person></presence>
我在Android中遗漏了什么或做错了什么?


提前感谢。

我犯了一个大错误:我试图解析byte[]数据,执行
新字符串(内容)
解决了我的问题

该解决方案的可能副本仍然不适用于我
public String getContent() {

    StringWriter sw = new StringWriter();

    try {

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(pidfContent), new StreamResult(sw));

        return sw.toString();
    } catch (TransformerConfigurationException ex) {
        Logger.getLogger(PIDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TransformerException ex) {
        Logger.getLogger(PIDF.class.getName()).log(Level.SEVERE, null, ex);
    }

    return sw.toString();

}