简体   繁体   English

使用Java DOM解析器解析具有复杂结构的xml文件中的子元素

[英]parsing through a child element in an xml file with complex structure, using Java DOM Parser

I have to parse through XML Files with a complicated structure-- 我必须解析具有复杂结构的XML文件-

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... “ PA” -----------顶级组元素,包含ar,par,paes和一个以上元素...

---"ar" Group element --comprised of reel-no, frame-no, last-update-date, purge-indicator, recorded-date, page-count, correspondent, conveyance-text. ---“ ar”组元素-由卷轴号,帧号,最后更新日期,清除指示符,记录日期,页数,对应内容,传送文本组成。 In "ar" we require "last-update-date" and "recorded date" 在“ ar”中,我们需要“ last-update-date”和“ recorded date”

--- "pars" group element- comprised of one or more "pr" elements.--"pr" is a group element comprised of name, and execution-date. ---“ pars”组元素-由一个或多个“ pr”元素组成。-“ pr”是由名称和执行日期组成的组元素。

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. 请注意,从上方看,单个根记录中可能有一个或多个“ pr”元素,并且关系是root或PA-> pars->一个或多个pr元素。

Now I have already navigated to a single root element (ie element of PA) using the following code--- 现在,我已经使用以下代码导航到单个根元素(即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.
    }

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? 是否可以解析上面获得的单个元素(像复杂的xml结构那样处理该元素本身),以便获得“ pr”元素的节点列表? I already have a single "PA" element (within which the nodelist of "pr" elements should be). 我已经有一个“ PA”元素(“ pr”元素的节点列表应在其中)。 For your reference, the exact relation between "PA" and "pr" is root or PA --> pars --> one or more pr elements. 供您参考,“ PA”和“ pr”之间的确切关系是root或PA-> pars->一个或多个pr元素。

You could try with XPath, for example if you put something like this instead of "add code" comment: 您可以尝试使用XPath,例如,如果您放置这样的内容而不是“添加代码”注释:

XPath xpath = XPathFactory.newInstance().newXPath();            
NodeList list=(NodeList) xpath.evaluate("pars/pr", el, XPathConstants.NODESET);

This should give you a list of all pr nodes that are children of pars node under your current PA . 这应该给您列出当前PApars节点的子节点的所有pr节点的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM