在 Java 中读取和解析任意 xml 结构?
XML 解析新手。我需要读取一组任意 XML 文件。它们都应该是 POX,如以下格式:
<someThing>
<property1>blah blah blah</property1>
<property2>blah blah blah</property2>
</someThing>
或以下:
<thingList>
<items>
<otherThing>
<prop3>blah blah blah</prop3>
<prop4>blah blah blah</prop4>
</otherThing>
<otherThing>
<prop3>blah blah blah</prop3>
<prop4>blah blah blah</prop4>
</otherThing>
</items>
</thingList>
所以我事先不知道元素的名称。我的问题是是否有任何 Java 库可以不按节点名称或节点属性而是按结构来处理 XML 中的读取?
New to XML parsing.I need to read a group of arbitrary XML files. They are all supposed to be POX, like the following format:
<someThing>
<property1>blah blah blah</property1>
<property2>blah blah blah</property2>
</someThing>
or the following:
<thingList>
<items>
<otherThing>
<prop3>blah blah blah</prop3>
<prop4>blah blah blah</prop4>
</otherThing>
<otherThing>
<prop3>blah blah blah</prop3>
<prop4>blah blah blah</prop4>
</otherThing>
</items>
</thingList>
So I won't know the name of the elements beforehand. My question is if there is any Java library that handles reading in XML not by the name of the node or the node's attribute, but by structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 getRootElement 方法可以获得 xml 文件的根元素。获取根元素后,您必须指定要获取的元素的 xml 路径。我已经使用它为“items/otherThing/prop3”编写了内容,您将获得 prop3 标签值。
Using getRootElement method you can get the root element of the xml file. After getting the root element you have to specify the xml path which the element you want to get. I have written for "items/otherThing/prop3" using this, you get your prop3 tag value.
您可以使用
getChildNodes()
获取整个节点列表,并在visitNode()
等方法中递归调用此方法。该 API 在 org.w3c.dom 包中可用。首先,通过我提到的
visitNode()
方法调用文档根节点上的getChildNodes()
,然后在visitNode()
中再次调用对当前节点中的每个子节点调用visitNode()
。您可以通过
getNodeType()
检查类型,如果是Node.TEXT_NODE
则访问getNodeValue()
以获取该标签的值。You can use the
getChildNodes()
to get the entire list of nodes, call this recursively in a method likevisitNode()
. This API is available in org.w3c.dom package.First you call the
getChildNodes()
on the document's root node through thevisitNode()
method I mentioned and then within thevisitNode()
you again callvisitNode()
on each of the child nodes in the current node.You can check the type by
getNodeType()
, and if it isNode.TEXT_NODE
access thegetNodeValue()
to get the value of that tag.我认为如果你想使用 Node,DOM 是最好的选择。您可以迭代每个节点及其子节点列表。这是很好的示例得到一个想法。
I think DOM is best option if you want to go by Node. You may iterate through each node and it's list of child nodes. Here is good example to get an idea.