使用 Java DOM Parser 解析具有复杂结构的 xml 文件中的子元素
我必须解析具有复杂结构的 XML 文件 -
下面给出的是结构一部分的简要摘要 -
“PA”------------ 顶级组元素,由 ar 组成, pars、paes 和另一个元素...
---"ar" 组元素 -- 由卷轴号、帧号、上次更新日期、清除指示器、记录日期、页数、通讯员组成,运输文本。在“ar”中,我们需要“last-update-date”和“recorded date”
---“pars”组元素-由一个或多个“pr”元素组成。--“pr”是由名称组成的组元素,和执行日期。
注意,从上面可以看出,单个根记录内可以有一个或多个“pr”元素,并且关系是root或PA-->1。参数 -->一个或多个 pr 元素。
现在我已经使用以下代码导航到单个根元素(即 PA 的元素)---
//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("PA");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {
//get the PA element
Element el = (Element)nl.item(i);
//now add code so as to obtain a node list of "pr" elements.
}
是否可以解析上面获得的单个元素(将该元素本身视为复杂的 xml 结构),以便我可以获取“pr”元素的节点列表?我已经有一个“PA”元素(“pr”元素的节点列表应该在其中)。供您参考,“PA”和“pr”之间的确切关系是 root 或 PA -->参数 -->一个或多个 pr 元素。
I have to parse through XML Files with a complicated structure--
Given below is a brief summary of one portion of the structure--
"PA"----------- Top level Group element that comprises of ar, pars, paes and one more element...
---"ar" Group element --comprised of reel-no, frame-no, last-update-date, purge-indicator, recorded-date, page-count, correspondent, conveyance-text. In "ar" we require "last-update-date" and "recorded date"
--- "pars" group element- comprised of one or more "pr" elements.--"pr" is a group element comprised of name, and execution-date.
Note that from above, there may be one or more "pr" elements within a single root record and relation is root or PA --> pars --> one or more pr elements.
Now I have already navigated to a single root element (ie element of PA) using the following code---
//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("PA");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {
//get the PA element
Element el = (Element)nl.item(i);
//now add code so as to obtain a node list of "pr" elements.
}
Is it possible to parse through the single element obtained above, (treating that element itself like a complex xml structure) so that I can obtain the nodelist of "pr" elements? I already have a single "PA" element (within which the nodelist of "pr" elements should be). For your reference, the exact relation between "PA" and "pr" is root or PA --> pars --> one or more pr elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 XPath,例如,如果您放置类似这样的内容而不是“添加代码”注释:
这应该为您提供作为
pars
pr 节点的列表> 当前PA
下的节点。You could try with XPath, for example if you put something like this instead of "add code" comment:
This should give you a list of all
pr
nodes that are children ofpars
node under your currentPA
.