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

时间:2022-12-01 13:38:55

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,pars,paes和另外一个元素......

---"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"

---“ar”组元素 - 包括卷轴号,帧号,最后更新日期,清除指示符,记录日期,页数,通讯员,传送文本。在“ar”中我们需要“last-update-date”和“record 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”是由name和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.

请注意,从上面看,单个根记录中可能有一个或多个“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? 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.

是否有可能解析上面获得的单个元素(将该元素本身视为复杂的xml结构),以便我可以获得“pr”元素的节点列表?我已经有一个“PA”元素(其中“pr”元素的节点列表应该是)。供您参考,“PA”和“pr”之间的确切关系是root或PA - > pars - >一个或多个pr元素。

1 个解决方案

#1


1  

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.

这应该为您提供当前PA下的所有pr节点的列表,这些节点是pars节点的子节点。

#1


1  

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.

这应该为您提供当前PA下的所有pr节点的列表,这些节点是pars节点的子节点。