解析错误:消息:找到:字符,预期为 START_ELEMENT 或 END_ELEMENT

问题描述

我有一个大约 6 GB 的 XML,我需要根据宽度和高度值修改图像标签下的操作。 使用一些 SO 文章的帮助,我正在使用 STAX 编写代码,其中我为每个标签调用一个函数。作为 Java 和 XML 解析的初学者,一开始我正在编写基本代码。 在到达第 3 个标签后尝试获取 Item 下的子元素时,出现解析错误

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[6,50] 消息:找到:字符,预期为 START_ELEMENT 或 END_ELEMENT 在 com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.nextTag(来源不明)

在原始 XML 中,它在这一行给出了这个错误

<carepacknr label="Care Pack number">xxx</carepacknr>

我的 XML 是经过验证的 XML,如何处理这些 CHaraCTERS 问题? 我也尝试过编码,但无济于事: 我的代码是:

  public class TransformWithStax
{

  @SuppressWarnings( "resource" )
  public static void main( String[] args ) throws XMLStreamException,FactoryConfigurationError,IOException
  {

    File file = new File( "C://abc//xyz.xml" );
    XMLInputFactory factory = XMLInputFactory.newFactory();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( Files.readAllBytes( file.toPath() ) );
    XMLStreamReader reader = factory.createXMLStreamReader( byteArrayInputStream,"UTF-8" );
    //factory.setProperty( XMLInputFactory.SUPPORT_DTD,false );
    reader.nextTag();
    QName name = reader.getName();
    System.out.println( "reader.nextTag() " + name.getLocalPart() ); //$NON-NLS-1$
    readRoot( reader );

  }

  private static void readRoot( XMLStreamReader reader ) throws XMLStreamException
  {
    //System.out.println( "In read Root" ); //$NON-NLS-1$
    while ( reader.nextTag() == XMLEvent.START_ELEMENT )
    {
      System.out.println( "In read Root inside while to check images" ); //$NON-NLS-1$
      QName name = reader.getName();
      System.out.println( "In read Root inside while and name is: " + name.getLocalPart() ); //$NON-NLS-1$
      if ( name.getLocalPart()
               .equals( "images" ) ) //$NON-NLS-1$
      {
        System.out.println( "In read Root inside if to check images" ); //$NON-NLS-1$
        readImages( reader );

      }
      /*else
      {
        reader.nextTag();
      }*/
      //readImages( reader );
      reader.nextTag();
    }

  }

  private static void readImages( XMLStreamReader reader ) throws XMLStreamException
  {
    while ( reader.nextTag() == XMLEvent.START_ELEMENT )
    {
      QName name = reader.getName();

      if ( !name.getLocalPart()
                .equals( "image" ) ) //$NON-NLS-1$
      {
        throw new XMLStreamException( name.toString() );
      }
      reader.nextTag();
      readData( reader );
      reader.nextTag();
    }

  }

  private static void readData( XMLStreamReader reader ) throws XMLStreamException
  {
    reader.nextTag();
    System.out.println( "width " + reader.getElementText() ); //$NON-NLS-1$
    reader.nextTag();
    System.out.println( "height: " + reader.getElementText() ); //$NON-NLS-1$
    reader.nextTag();
    System.out.println( "url: " + reader.getElementText() ); //$NON-NLS-1$

  }

}

我的 XML 示例是:

<Items>
   <Item >
      <Tag1>
      </Tag1>
         <Tag2>
        </Tag2>
            <Images>
               <Image>
                  <width>200</width>
                  <height>200</height>
                  <url>xyz.com</url>
                  <action>update</action>
               </Image>
               <Image>
                  <width>400</width>
                  <height>600</height>
                  <url>xyz.com</url>
                  <action>update</action>
               </Image>
            </Images>
   </Item>
   <Item >
      <Tag1>
      </Tag1>
         <Tag2>
        </Tag2>
            <Images>
               <Image>
                  <width>100</width>
                  <height>400</height>
                  <url>abc.com</url>
                  <action>update</action>
               </Image>
               <Image>
                  <width>400</width>
                  <height>200</height>
                  <url>xyz.com</url>
                  <action>update</action>
               </Image>
            </Images>
   </Item>
</Items>
任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...