如何在Android中解析XML文件?
我有一个XML文件,例如
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
我需要全路径
值,即“ oebps/content.opf”文本。我尝试使用文档构建器和XML解析器,但没有结果。我如何穿越该节点并获得值
I have an XML file like
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
I need the full-path
value that is "OEBPS/content.opf" text. I tried using Document builder and XML parser but no results. How do I traverse to that node and get the value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一种访问属性值的方法。我已经解释了评论中的所有行。
I found a way to access the attribute values. I have explained all the lines in the comments.
我已经使用了 jackson serialize serialize and deSerialize(parse)xml。我为每个元素制作一个pojo,然后根据需要使用其解析的注释。以下是一个粗略的例子。但是,这两个外部类将使用
@jsonignoreproperties
注释忽略所有内容(除非您将其包含在POJO中)。rootfile
类将使用@jacksonxmlproperty
注释来解析您想要的属性。Sidenote:我不是
@jsonrootname
的100%。这就是我标记XML root的方式,但是我的同事使用@jackonxmlrootlement(value =“ ...”)将XML字符串解析为POJO
>I've used Jackson to serialize and deserialize(parse) xml. I make a POJO for each element and then using the annotations it parses to the POJO as needed. The below is a rough example. But the the two outer classes will ignore everything(unless you include it in the POJO) using the
@JsonIgnoreProperties
annotations. And theRootfile
class will parse the attribute you want using the@JacksonXmlProperty
annotation.Sidenote: I'm not 100% on the
@JsonRootName
. That is how I marked the xml root, but my colleague parsed the xml string to POJO using@JackonXmlRootElement(value = "...")