Parsing simple sip messages with XPath on android
I am trying to parse simple SIP messages using XPath and Android. One of this messages is described in this document.
The actual message looks like this:
<isComposing xmlns='urn:ietf:params:xml:ns:imiscomposing'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<state>active </state>
<contenttype> text/html </contenttype>
<refresh>60</refresh>
</isComposing>
I have this block of code that get the message from the SIP request and tries to parse it in order to find the state,
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false); // never forget this! THIS WAS TRUE ORIGINALLY CHANGED BY MAXSAP
DocumentBuilder builder;
try {
builder = domFactory.newDocumentBuilder();
Log.e("IMHandler","<onTransRequest> parsing body:: "+ req.getBody());
Document doc = builder.parse(req.getBody());
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr= xpath.compile("//isComposing/state/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Log.e("IMHandler"," <onTransRequest> *************** PARSING ***********");
Log.e("IMHandler"," <onTransRequest> NodeValue"+nodes.item(i).getNodeValue());
Log.e("IMHandler"," <onTransRequest> *************** /PARSING ***********");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but in this line builder.parse(req.getBody()); I am getting the error:
03-20 20:03:44.637: WARN/System.err(4150): java.net.MalformedURLException: Protocol not found: <SPAN STYLE="FONT-FAMILY:Arial; FONT-SIZE:10pt ">dfg</SPAN>
03-20 20:03:44.637: WARN/System.err(4150): at java.net.URL.<init>(URL.java:275)
03-20 20:03:44.637: WARN/System.err(4150): at java.net.URL.<init>(URL.java:159)
03-20 20:03:44.637: WARN/System.err(4150): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:120)
03-20 20:03:44.637: WARN/System.err(4150): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:158)
03-20 20:03:44.637: WARN/System.err(4150): at org.sipdroid.sipua.ui.IMHandler.onTransRequest(IMHandler.java:239)
03-20 20:03:44.637: WARN/System.err(4150): at org.zoolu.sip.transaction.TransactionServer.onReceivedMessage(TransactionServer.java:148)
03-20 20:03:44.637: WARN/System.err(4150): at org.zoolu.sip.provider.SipProvider.processReceivedMessage(SipProvider.java:1091)
03-20 20:03:44.637: WARN/System.err(4150): at org.zoolu.sip.provider.SipProvider.onReceivedMessage(SipProvider.java:1203)
03-20 20:03:44.637: WARN/System.err(4150): at org.zoolu.sip.provider.UdpTransport.onReceivedPacket(UdpTransport.java:117)
03-20 20:03:44.637: WARN/System.err(4150): at org.zoolu.net.UdpProvider.run(UdpProvider.java:189)
Has anyone encountered the same error? I am new to using XPath, also n开发者_如何学运维ote that I have changed the namespace awarnes to hoping that this was a name space problem but no luck.
While req.getBody
is processed to create a stream (?) to parse, some URL interactions is taking place. Your code us using a malformed URL.
Twice check that the URL is used to create a req
object is correctly formed one.
EDIT:
The method DocumentBuilder.parse(String) is parsing a document from a specified URL. Then, a getBody()
method returning a String
and DocumentBuilder
trying to interprete it as a string URL. You need to use parse(InputStream) or parse(File) in your case.
EDIT: You need to specify namespace context for your XPath operation. Try to add these lines before evaluating an expression.
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String s) {
if (s.equals(XMLConstants.DEFAULT_NS_PREFIX))
return doc.lookupNamespaceURI(null);
else
return doc.lookupNamespaceURI(s);
}
public String getPrefix(String s) {
return doc.lookupPrefix(s);
}
public Iterator getPrefixes(String s) {
return null;
}
});
精彩评论