【发布时间】:2017-02-27 09:55:54
【问题描述】:
我在解析包含 Unicode 和拉丁字符作为 XML 数据一部分的 XML 数据时遇到问题。 它抛出一个错误,说无法解析输入的 XML 数据。 请找到附加的代码 sn-p 并做必要的解决问题。
这是我们在应用程序中传递的输入
String url = "<?xml version='1.0' encoding='UTF-8'?><candidate-registrations customer-id='197'>
<registration-details method=' '>
<candidate-demographics>
<candidate-details>
<candidate-id-type value='SSN'/>
<candidate-id value='567876456'/>
<first-name value='ยง'/>
<last-name value='mohan'/>
<date-of-birth value='03/03/1980'/>
<email-address value='jagannatha.venkataravanappa@harman.com'/>
<school-code>0129</school-code>
</candidate-details>
</candidate-demographics>
</registration-details></candidate-registrations>";
这是我们正在使用的代码
private XMLReader xr;
public SaxMapper( )
{
try
{
// Create the XML reader...
xr = XMLReaderFactory.createXMLReader();
}
catch(Exception e)
{
LoggerManager.Log(LogLevelConstants.INFO, className, "SaxMapper", e.getMessage(),e);
}
}
public Object fromXML( String url )
{
try
{
return fromXML( new InputSource( url ));
}
catch ( Exception e )
{
LoggerManager.Log(LogLevelConstants.INFO, className, "fromXML", e.getMessage(),e);
return null;
}
}
private synchronized Object fromXML( InputSource in ) throws Exception
{
// Set the ContentHandler...
xr.setContentHandler( this );
// Parse the file...
xr.parse( in );
return getMappedObject();
}
这是我遇到的错误,
Error : <?xml version="1.0" encoding="UTF-8"?><import-results><result>BAD</result><reason-code>100</reason-code><reason-desc>Unable to parse the input XML</reason-desc><error>Unable to parse the input XML</error></import-results>
【问题讨论】:
-
您没有包含错误,并且您的代码非常需要改进格式。此外,Java 不允许多行字符串,因此您的 XML 字符串代码甚至无法编译。 并且您的代码省略了非常必要的类声明和范围。
-
即使我知道 java 不支持多行字符串,为了便于阅读,我已经格式化了字符串并发布在这里
-
我已经添加了错误,检查并告诉我
标签: java unicode xml-parsing