解析DOM - org.xml.sax.SAXParseException

时间:2012-08-18 01:07:08

标签: java parsing dom

这就是我要解析的内容,

<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
<newPlayerConnectionrequest>
    <‌​participantType>1</participantType>
</newPlayerConnectionrequest>

创建消息的客户端代码:

Element rootElement = document.createElement(MyServer.NEW_PLAYER_CONNECTION_REQUEST);
    Element participantTypeElement = document.createElement(MyServer.PARTICIPANT_TYPE);
    Text textParticipantType = document.createTextNode(participantType);
    participantTypeElement.appendChild(textParticipantType);
    rootElement.appendChild(participantTypeElement);
    document.appendChild(rootElement);
    StringWriter sw = new StringWriter();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(sw);
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    catch (TransformerException t) {
        t.printStackTrace();
    }
    try {
        conn.setRequestMethod("POST");
    } catch (ProtocolException e) {
        e.printStackTrace();
    }
    conn.setRequestProperty("Content-Type", "text/xml");
    //conn.setRequestProperty("Content-Length", "" +  8);
    conn.setRequestProperty("Content-Language", "en-US");  
    conn.setDoInput(true);
    conn.setDoOutput(true

我收到以下错误:

White space is required between the processing instruction target and data.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 13;
    White space is required between the processing instruction target and data.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:253)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:288)
at MyServer.doPost(MyServer.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:643)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:450)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1067)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:377)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1001)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:129)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
at org.eclipse.jetty.server.Server.handle(Server.java:360)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)

编辑我要解析的字符串:

1 个答案:

答案 0 :(得分:8)

此行错误

<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>

应该阅读

<?xml version="1.0"encoding="UTF-8"standalone="no"?>

请注意“xml”和“version”之间的空格

Google是你的朋友;)