简体   繁体   English

如何在Java中读取XML样式的文本文件?

[英]How to read XML styled text file in java?

I want to read below XML file in Java which is in text format, 我想阅读以下Java格式的XML文件,该文件为文本格式,

XML File: XML档案:

    <Sample>
     <DATA>
       <ID>12345</ID>
       <NAME>Person1</NAME>
       <MAIL>sa@gmail.com</MAIL>
       <Class>A&B</Class>
    </DATA>
    <DATA>
       <ID>12345</ID>
       <NAME>Person1</NAME>
       <MAIL>sa@gmail.com</MAIL>
       <Class>A<B</Class>
    </DATA>
   </Sample>

I was used JAXB to read this xml file, for this i have added a POJO class too with annotation. 我曾经用JAXB来读取这个xml文件,为此我也添加了带有注释的POJO类。 While unmarshalling the XMl file am getting a below exception 在解组XMl文件时出现以下异常

[org.xml.sax.SAXParseException : The entity must immediately follow the '&' in the entity reference.]

Can any one suggest me a best approach to read this xml? 有人可以建议我阅读此xml的最佳方法吗?

See link - as stated by the error, the XML expects an entity reference. 参见链接 -如错误所述,XML需要实体引用。 You'll need to use &amp; 您需要使用&amp; , and &gt; &gt; for that greater-than symbol. 对于那个大于号。 The Saxon library you are using should process these URLencoded characters correctly. 您正在使用的Saxon库应该正确处理这些URL编码的字符。

When the XML parser finds an ampersand in the XML data, it expects to find a symbol name and a semicolon following it. 当XML解析器在XML数据中找到一个&符时,它期望在其后找到一个符号名和一个分号。 The symbol name provides a symbolic reference to another entity or character such as the ampersand, greater-than, and less-than characters. 符号名称提供对另一个实体或字符的符号引用,例如,“&”号,大于和小于字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM