0

I'm trying to parse a String to a Document. The content of the string is an XML data that I get from a SIP message.

I try to do this:

DocumentBuilder db;
Document pidf;

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

pidf = db.parse(is);

But I get this error:

org.xml.sax.SAXParseException: Unexpected token (position:TEXT [B@fd41b3b@1:11 in java.io.StringReader@83dc258) 

I use Log.d() to see the content of the string, and I can see this:

<?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>

I copy and paste to a validator webpage and it passed the validation.

Because of error output I think that it could be a format error when parsing, I tried to add is.setEncoding("UTF-8") to solved it but I still getting same error.

I created that string from an XML on a Java program, this is the code:

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();

}

What I'm missing or doing wrong in Android?

Thanks in advance.

Josete Manu
  • 187
  • 2
  • 16

1 Answers1

0

I was making a big mistake: I was trying to parse byte[] data, doing new String(content) solved my problem!

Josete Manu
  • 187
  • 2
  • 16