【问题标题】:unable to parse latin and unicode characters using XMLReader.parse() [closed]无法使用 XMLReader.parse() 解析拉丁和 unicode 字符 [关闭]
【发布时间】: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


【解决方案1】:

如果您阅读documentation on InputSource,您会注意到

new InputSource(String)

做一些与你预期不同的事情。

要使用 sax 解析字符串,请参阅:https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

请注意,给定的教程侧重于解析文件,而不是给定的字符串。不过明白了之后,就很容易改造了。

【讨论】:

    猜你喜欢
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多