【问题标题】:Reading XML with namespace using Apache Beam XmlIO使用 Apache Beam XmlIO 读取带有命名空间的 XML
【发布时间】:2021-10-30 14:37:37
【问题描述】:

我正在尝试将 XML 文件读入 Apache Beam 管道。一些元素具有命名空间,命名空间声明在根节点处声明。我能够使用标准 JAXB 解析器在 Apache Beam 之外解析 xml。但是,当我将 XmlIO.read() 函数与梁一起使用时,会出现以下异常:

com.ctc.wstx.exc.WstxParsingException:未声明的命名空间前缀“g”。

<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
 <channel>
  <item>
   <!-- Basic Product Information -->
   <g:id><![CDATA[SAMI9000NAVKIT]]></g:id>
   <title><![CDATA[Original Samsung Galaxy S i9000 Navigation Kit]]></title>
   <link><![CDATA[https://www.mobileciti.com.au/original-samsung-galaxy-s-i9000-navigation-kit]]></link>
   <description><![CDATA[<p>SAMSUNG Galaxy S (i9000) Navigation Kit - Consists of handset cradle, window shield mount and car charger.</p>]]></description>
   <g:product_category><![CDATA[Electronics > Communications > Telephony > Mobile Phone Accessories]]></g:product_category>
   <g:product_type><![CDATA[Accessories > Car Kits]]></g:product_type>
....
  </item>
 </channel>
</rss>

束码:

                .from(<Full file path>)
                .withRootElement("rss")
                .withRecordElement("item").withRecordClass(Item.class));

没有命名空间的 XML 可以正常工作。任何指针都非常感谢。谢谢

【问题讨论】:

    标签: java xml namespaces apache-beam


    【解决方案1】:

    看看 XmlSource code,不幸的是,如果你只指定一个根元素,我认为它默认不支持 XML 命名空间。

    不过,作为一种解决方法,您可以尝试执行以下操作:

    .withRootElement("rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\"")
    

    也许它会起作用。

    【讨论】:

    • 感谢@alexey-romanenko。上述解决方案有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多