开发者

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

开发者 https://www.devze.com 2023-04-05 07:05 出处:网络
I have to parse through XML Files with a complicated structure-- Given below is a brief summary of one portion of the structure--

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)开发者_如何学Go 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.


You could try with XPath, for example if you put something like this instead of "add code" comment:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号