解析DOM - org.xml.sax.SAXParseException(Parsing DO

2019-09-21 07:51发布

这就是我试图解析,

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

编辑我试图解析字符串:

Answer 1:

这条线是错误的

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

它应该阅读

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

注意“XML”和“版本”之间的空间

谷歌是你的朋友;)



文章来源: Parsing DOM - org.xml.sax.SAXParseException
标签: java parsing dom