java javax.xml.bind.UnmarshalException - 带有链接异常:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25154587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:27:40  来源:igfitidea点击:

javax.xml.bind.UnmarshalException - with linked exception:

javaxmlxmlstreamreader

提问by Sara

I have got receive an xml inputstream with a URLConnection. However when i try to unmarshall the inputstream. It throw an exception. javax.xml.bind.UnmarshalException - with linked exception: [javax.xml.stream.XMLStreamException: ParseError at [row,col]:[53088,40] Message: The reference to entity "T" must end with the ';' delimiter.]

我收到了一个带有 URLConnection 的 xml 输入流。但是,当我尝试解组输入流时。它抛出异常。javax.xml.bind.UnmarshalException - 带有链接异常:[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[53088,40] 消息:对实体“T”的引用必须以“;”结尾 分隔符。]

CODE:

代码:

try {
    URL url = new URL(hostURL);  
    URLConnection urlConnection = url.openConnection();  
    urlConnection.setRequestProperty("Accept", "application/xml");
    urlConnection.setRequestProperty("Authorization", "Basic " + authDetails);

    InputStream inputStream = urlConnection.getInputStream();

    XMLInputFactory xif = XMLInputFactory.newInstance();
    xif.setProperty("javax.xml.stream.isCoalescing", true); 
    XMLStreamReader xsr = xif.createXMLStreamReader(inputStream);

     // Advance to the "Packages" element.
     while(xsr.hasNext()) {
         if(xsr.isStartElement() && "packages".equals(xsr.getLocalName())) {
             break;
         }
         xsr.next();
      }

     JAXBContext jaxbContext = JAXBContext.newInstance(Packages.class);
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
     packages = (Packages) jaxbUnmarshaller.unmarshal(xsr);
    }catch (JAXBException e) {
     e.printStackTrace();
    }catch (MalformedURLException e) {
     e.printStackTrace();
    }catch (IOException e) {
     e.printStackTrace();
    }catch(Exception e){
     e.printStackTrace();
     }finally{

    }

PS: [row,col]:[53088,40] = newproductsite for T&T evol

PS: [row,col]:[53088,40] = T&T evol 的新产品站点

you can find Stack traces in link http://pastebin.com/LuhbnMQq

您可以在链接http://pastebin.com/LuhbnMQq 中找到堆栈跟踪

Guys am new to this thing and stuck with it, any help would be appreciated

伙计们是这件事的新手并坚持下去,任何帮助将不胜感激

BR Sara

BR莎拉

回答by laune

Your XML file isn't well-formed.

您的 XML 文件格式不正确。

newproductsite for T&T evol

You cannot have a '&' in the XML file, it must be represented as

XML 文件中不能有“&”,它必须表示为

&

How was the XML produced? The error is there...

XML 是如何产生的?错误就在那里...

回答by Ralf Wagner

Your xml input is not well formed. In the text newproductsite for T&T evolthe ampersand is a reserved character. It is used for escaping (e.g. &lt;represents the <sign or &#x41;is the letter A. So, after the &in the text, the parser expects a value and a ;to see that the escape sequence is finished. If you want to use the ampersand in your xml elements, it must be escaped e.g. by &amp;.

您的 xml 输入格式不正确。在文本中,newproductsite for T&T evol与号是保留字符。它用于转义(例如&lt;表示<符号或是&#x41;字母A。因此,在&文本中的之后,解析器需要一个值和 a;以查看转义序列已完成。如果您想在 xml 元素中使用&符号,它必须被转义,例如&amp;

To fix the problem the source needs to produce either well-formed xml or you need to sanitize the data you received before processing it. I would recommend the first.

要解决此问题,源需要生成格式良好的 xml,或者您需要在处理之前清理收到的数据。我会推荐第一个。