例外:读取 XML 时没有协议

Posted

技术标签:

【中文标题】例外:读取 XML 时没有协议【英文标题】:Exception: No protocol while reading XML 【发布时间】:2010-11-01 03:33:14 【问题描述】:

我需要从字符串缓冲区或字符串中解析 xml 数据..我的代码如下。 在线document doc =db.parse(eventXml ) ..它抛出异常-请在下面找到代码和异常。请帮助我

代码

eventXml = strBuffer.toString();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(eventXml );

例外

 java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8
    " standalone="yes"?>%0A<EventInfo xmlns="http://www.telenet.be/oms/event">%0A
     <TelenetEventInfo className="be.telenet.oms.events.OmsAsapJsrpNotifyEvent">%0A
           <SimpleFields>%0A            <SimpleField>%0A                <FieldName>C
    ompletion_Date_Time</FieldName>%0A                <FieldValue>04/08/2009 08:34:0
    1</FieldValue>%0A            </SimpleField>%0A            <SimpleField>%0A
              <FieldName>Originator</FieldName>%0A                <FieldValue>System
    </FieldValue>%0A            </SimpleField>%0A            <SimpleField>%0A
             <FieldName>Status</FieldName>%0A                <FieldValue>S</FieldVal
    ue>%0A            </SimpleField>%0A            <SimpleField>%0A                <
    FieldName>Workorder_ID</FieldName>%0A                <FieldValue>I00054132231-09
    8</FieldValue>%0A            </SimpleField>%0A        </SimpleFields>%0A
    <ArrayData>%0A            <ArrayNames>%0A                <ArrayName>Parameters</
    ArrayName>%0A                <ArrayFieldEntry>%0A                    <ArraySubFi
    eld>[0].Parameter_Name</ArraySubField>%0A                    <ArraySubFieldValue
    >WARNING_TEXT</ArraySubFieldValue>%0A                </ArrayFieldEntry>%0A
              <ArrayFieldEntry>%0A                    <ArraySubField>[0].Parameter_v
    alue</ArraySubField>%0A                    <ArraySubFieldValue>UnknownKeyExcepti
    on-Retrieving a webURL%0A                    for an unknown loginid: a004301</Ar
    raySubFieldValue>%0A                </ArrayFieldEntry>%0A                <ArrayF
    ieldEntry>%0A                    <ArraySubField>[1].Parameter_Name</ArraySubFiel
    d>%0A                    <ArraySubFieldValue>AGED</ArraySubFieldValue>%0A
             </ArrayFieldEntry>%0A                <ArrayFieldEntry>%0A
          <ArraySubField>[1].Parameter_value</ArraySubField>%0A                    <
    ArraySubFieldValue/>%0A                </ArrayFieldEntry>%0A                <Arr
    ayFieldEntry>%0A                    <ArraySubField>[2].Parameter_Name</ArraySubF
    ield>%0A                    <ArraySubFieldValue>OPERATION</ArraySubFieldValue>%0
    A                </ArrayFieldEntry>%0A                <ArrayFieldEntry>%0A
                  <ArraySubField>[2].Parameter_value</ArraySubField>%0A
           <ArraySubFieldValue>DEL</ArraySubFieldValue>%0A                </ArrayFie
    ldEntry>%0A                <ArrayFieldEntry>%0A                    <ArraySubFiel
    d>[3].Parameter_Name</ArraySubField>%0A                    <ArraySubFieldValue>S
    USPEND</ArraySubFieldValue>%0A                </ArrayFieldEntry>%0A
       <ArrayFieldEntry>%0A                    <ArraySubField>[3].Parameter_value</A
    rraySubField>%0A                    <ArraySubFieldValue/>%0A                </Ar
    rayFieldEntry>%0A            </ArrayNames>%0A        </ArrayData>%0A        <eve
    ntName>OMS::ASAP::JSRP::Notify</eventName>%0A        <destination>OSB</destinati
    on>%0A    </TelenetEventInfo>%0A</EventInfo>%0A%0A
            at java.net.URL.<init>(URL.java:537)
            at java.net.URL.<init>(URL.java:434)
            at java.net.URL.<init>(URL.java:383)
            at weblogic.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityMan
    ager.java:836)
            at weblogic.apache.xerces.impl.XMLEntityManager.startDocumentEntity(XMLE
    ntityManager.java:782)
            at weblogic.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(XML
    DocumentScannerImpl.java:260)
            at weblogic.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguratio
    n.java:499)
            at weblogic.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguratio
    n.java:581)
            at weblogic.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
            at weblogic.apache.xerces.parsers.DOMParser.parse(DOMParser.java:257)
            at weblogic.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilder
    Impl.java:201)
            at weblogic.xml.jaxp.RegistryDocumentBuilder.parse(RegistryDocumentBuild
    er.java:149)
            at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
            at Controller.getEvent_Xml(Controller.jpf:822)
            at Controller.GetDetails(Controller.jpf:487)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:324)
            at com.bea.wlw.netui.pageflow.FlowController.invokeActionMethod(FlowCont
    roller.java:1512)
            at com.bea.wlw.netui.pageflow.FlowController.getActionMethodForward(Flow
    Controller.java:1447)
            at com.bea.wlw.netui.pageflow.FlowController.internalExecute(FlowControl
    ler.java:778)
            at com.bea.wlw.netui.pageflow.PageFlowController.internalExecute(PageFlo
    wController.java:211)
            at com.bea.wlw.netui.pageflow.FlowController.execute(FlowController.java
    :608)
            at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
    tProcessor.java:484)
            at com.bea.wlw.netui.pageflow.PageFlowRequestProcessor.processActionPerf
    orm(PageFlowRequestProcessor.java:1504)
            at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
    va:274)
            at com.bea.wlw.netui.pageflow.PageFlowRequestProcessor.process(PageFlowR
    equestProcessor.java:674)
            at com.bea.wlw.netui.pageflow.AutoRegisterActionServlet.process(AutoRegi
    sterActionServlet.java:527)
            at com.bea.wlw.netui.pageflow.PageFlowActionServlet.process(PageFlowActi
    onServlet.java:152)
            at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

            at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run
    (ServletStubImpl.java:1072)
            at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    pl.java:465)
            at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
    pl.java:348)
            at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio
    n.run(WebAppServletContext.java:6985)
            at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticate
    dSubject.java:321)
            at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:
    121)
            at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
    rvletContext.java:3892)
            at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
    pl.java:2766)
            at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
            at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
      Closing down all connections...

    <Jun 8, 2009 4:03:18 PM IST> <Error> <HTTP> <BEA-101019> <[ServletContext(id=312
    55131,name=ECToolWeb,context-path=/ECToolWeb)] Servlet failed with IOException
    java.net.SocketTimeoutException: Read timed out
            at java.net.SocketInputStream.socketRead0(Native Method)
            at java.net.SocketInputStream.read(SocketInputStream.java:129)
            at weblogic.servlet.internal.PostInputStream.read(PostInputStream.java:1
    70)
            at weblogic.servlet.internal.ServletInputStreamImpl.read(ServletInputStr
    eamImpl.java:180)
            at weblogic.servlet.internal.ServletRequestImpl.mergePostParams(ServletR
    equestImpl.java:1339)
            at weblogic.servlet.internal.ServletRequestImpl.parseQueryParams(Servlet
    RequestImpl.java:1206)
            at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequ
    estImpl.java:1409)
            at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequ
    estImpl.java:1395)
            at com.bea.wlw.netui.pageflow.scoping.ScopedServletUtils.getScopedSessio
    nAttrName(ScopedServletUtils.java:301)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.getCurrentActionResolver(Pag
    eFlowUtils.java:496)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.getCurrentPageFlow(PageFlowU
    tils.java:478)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.ensureCurrentPageFlow(PageFl
    owUtils.java:454)
            at com.bea.wlw.netui.pageflow.PageFlowJspFilter.doFilter(PageFlowJspFilt
    er.java:193)
            at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.ja
    va:27)
            at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio
    n.run(WebAppServletContext.java:6987)
            at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticate
    dSubject.java:321)
            at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:
    121)
            at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
    rvletContext.java:3892)
            at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
    pl.java:2766)
            at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
            at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
    --------------- nested within: ------------------
    weblogic.utils.NestedRuntimeException: Cannot parse POST parameters of request:
    '/ECToolWeb/DetailDisplay.jsp' - with nested exception:
    [java.net.SocketTimeoutException: Read timed out]
            at weblogic.servlet.internal.ServletRequestImpl.mergePostParams(ServletR
    equestImpl.java:1364)
            at weblogic.servlet.internal.ServletRequestImpl.parseQueryParams(Servlet
    RequestImpl.java:1206)
            at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequ
    estImpl.java:1409)
            at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequ
    estImpl.java:1395)
            at com.bea.wlw.netui.pageflow.scoping.ScopedServletUtils.getScopedSessio
    nAttrName(ScopedServletUtils.java:301)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.getCurrentActionResolver(Pag
    eFlowUtils.java:496)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.getCurrentPageFlow(PageFlowU
    tils.java:478)
            at com.bea.wlw.netui.pageflow.PageFlowUtils.ensureCurrentPageFlow(PageFl
    owUtils.java:454)
            at com.bea.wlw.netui.pageflow.PageFlowJspFilter.doFilter(PageFlowJspFilt
    er.java:193)
            at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.ja
    va:27)
            at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio
    n.run(WebAppServletContext.java:6987)
            at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticate
    dSubject.java:321)
            at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:
    121)
            at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
    rvletContext.java:3892)
            at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
    pl.java:2766)
            at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
            at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
    >
.

【问题讨论】:

jrharshath:虽然我同意你的观点,但你的态度不属于这里。 相关***.com/questions/1706493/… 【参考方案1】:

你正在调用

Document doc = db.parse(eventXml);

eventXml 显然是一串 XML 数据。

然而,DocumentBuilder.parse(String) 方法需要一个 URI 来加载 XML。这就是您收到 MalformedURLException 的原因:Java 尝试使用您的 XML 作为 URI。

您需要 DocumentBuilder.parse(InputSource) 或 DocumentBuilder.parse(InputStream)。 InputStream 可以使用例如从您的 String/StringBuffer 创建

new InputSource(new StringReader(xmlString))

(未经测试)。

【讨论】:

+1 经过测试,它可以工作。不推荐使用 StringBufferInputStream。【参考方案2】:

parse() 不采用实际的 XML 作为参数,而是采用指向文档的 URL。

你应该使用 DocumentBuilder.parse(new StringBufferInputStream(eventXml));

【讨论】:

(+1) .. 清楚地写在 DocumentBuilder#parse 的 JavaDoc 中。如果仍有疑问,请在 Google 上搜索“DocumentBuilder”并点击第一个链接。 StringBufferInputStream 是 deprecated。有更好的解决方案:***.com/a/1706533/1851290 您可以使用new ByteArrayInputStream(eventXml.getBytes("utf-8")) 替换已弃用的StringBufferInputStream【参考方案3】:

试试db.parse(eventXml),而不是:

Document doc = db.parse(new InputSource(new StringReader(eventXml)));

【讨论】:

【参考方案4】:

您发布了如此多的内容,但没有任何内容可以帮助任何读者了解您想要做什么。 如果您的问题只是 XML 解析给您带来了问题,请检查您的 XML 格式是否正确。

【讨论】:

【参考方案5】:

这个 sn-p 应该可以工作。

private String updateMyXML(String orderXML) 
    javax.xml.parsers.DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inputSource = new InputSource(new StringReader(orderXML));

【讨论】:

以上是关于例外:读取 XML 时没有协议的主要内容,如果未能解决你的问题,请参考以下文章

客户端检测到一个协议错误,代码 0x1104是啥意思

NSXMLParser读取XML文件并将数据显示到TableView上

客户端检测到一个协议错误,代码 0x1104是啥意思

Tomcat-Ajp协议文件读取漏洞(CVE-2020-1938)

UDS(ISO14229-2006) 汉译(No.7 应用层协议)未完,待续

读取 RTSP 流时强制 FFMPEG 使用 TCP 协议