提问人:Jayabalan Kandasamy 提问时间:9/1/2021 最后编辑:Vadim KotovJayabalan Kandasamy 更新时间:9/3/2021 访问量:243
使用 Apache Beam XmlIO 读取带有命名空间的 XML
Reading XML with namespace using Apache Beam XmlIO
问:
我正在尝试将 XML 文件读入 Apache Beam 管道。某些元素具有命名空间,并且命名空间声明在根节点上声明。我能够使用标准的 JAXB 解析器解析 Apache Beam 之外的 xml。但是,当我将 XmlIO.read() 函数与 beam 一起使用时,我收到以下异常:
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 工作正常。非常感谢任何指示。谢谢
答:
0赞
Alexey Romanenko
9/3/2021
#1
不幸的是,查看 XmlSource 代码,如果您只指定根元素,我认为默认情况下它不支持 XML 命名空间。
不过,作为一种解决方法,您可以尝试执行如下操作:
.withRootElement("rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\"")
也许它会起作用。
评论
1赞
Jayabalan Kandasamy
9/16/2021
谢谢@alexey-romanenko。上述解决方案奏效了。
评论