You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Wayne Johnson <wd...@yahoo.com> on 2004/09/20 16:16:40 UTC

Searching for elements without decending into children

I have an XML structure where a specific element is referenced at two levels of the same tree:
 
<RuleTemplate>
  <RuleDefinition/>
  <ConcreteRule>
    <RuleDefinition/>
  </ConcreteRule>
</RuleTemplate>
 
I'd like to search for all the RuleDefinitions that are children of RuleTemplate, but not the one in ConcreteRule.  
 
The XML maps to the object structures so changing things is not an option.  
 
Up till now I was using getElementsByTagNamegetElementsByTagName, but recently realized it wasn't doing what I thought.  
 
I would have suspected that DOM would have another way to do this, but haven't found any, short of doing it by hand.  Since my interface specifies a NodeList, which I haven't figured out how to edit, I'm kind of stuck.
 
Any suggestions?  
 
Thanks.





---
Wayne Johnson,             | There are two kinds of people: Those 
3943 Penn Ave. N.          | who say to God, "Thy will be done," 
Minneapolis, MN 55412-1908 | and those to whom God says, "All right, 
(612) 522-7003             | then,  have it your way." --C.S. Lewis
		
---------------------------------
Do you Yahoo!?
vote.yahoo.com - Register online to vote today!

Re: Searching for elements without decending into children

Posted by Nikhil Dinesh <ni...@seas.upenn.edu>.
Here are two ways that I know of:

1. Use XPath. The Apache XPath API is a part of the Xalan distribution
2. The DOM Level2 traversal API ie org.w3c.dom.traversal. Here you
implement a NodeFilter which you can use in conjunction with a TreeWalker.

(1) above is the simplest, (2) is more low level and hence much faster.

-Nikhil

On Mon, 20 Sep 2004, Wayne Johnson wrote:

> I have an XML structure where a specific element is referenced at two levels of the same tree:
>
> <RuleTemplate>
>   <RuleDefinition/>
>   <ConcreteRule>
>     <RuleDefinition/>
>   </ConcreteRule>
> </RuleTemplate>
>
> I'd like to search for all the RuleDefinitions that are children of RuleTemplate, but not the one in ConcreteRule.
>
> The XML maps to the object structures so changing things is not an option.
>
> Up till now I was using getElementsByTagNamegetElementsByTagName, but recently realized it wasn't doing what I thought.
>
> I would have suspected that DOM would have another way to do this, but haven't found any, short of doing it by hand.  Since my interface specifies a NodeList, which I haven't figured out how to edit, I'm kind of stuck.
>
> Any suggestions?
>
> Thanks.
>
>
>
>
>
> ---
> Wayne Johnson,             | There are two kinds of people: Those
> 3943 Penn Ave. N.          | who say to God, "Thy will be done,"
> Minneapolis, MN 55412-1908 | and those to whom God says, "All right,
> (612) 522-7003             | then,  have it your way." --C.S. Lewis
>
> ---------------------------------
> Do you Yahoo!?
> vote.yahoo.com - Register online to vote today!

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Searching for elements without decending into children

Posted by Don Bate <do...@iadfw.net>.
Try something like:

Element elem = ?;
LinkedList result = new LinkedList ();
NodeList list = elem.getChildNodes ();
for (int i = 0; i < list.getLength; i++) {
   Node item = list.item (i);
   if ((item.getNodeType () == Node.ElementNode) && 
("RuleDefinition".equals (item.getLocalName ()))) {
     result.add (item);
   }
}

At 7:16 AM -0700 9/20/04, Wayne Johnson wrote:
>I have an XML structure where a specific element is referenced at 
>two levels of the same tree:
>
><RuleTemplate>
>   <RuleDefinition/>
>   <ConcreteRule>
>     <RuleDefinition/>
>   </ConcreteRule>
></RuleTemplate>
>
>I'd like to search for all the RuleDefinitions that are children of 
>RuleTemplate, but not the one in ConcreteRule.
>
>The XML maps to the object structures so changing things is not an option.
>
>Up till now I was using getElementsByTagNamegetElementsByTagName, 
>but recently realized it wasn't doing what I thought.
>
>I would have suspected that DOM would have another way to do this, 
>but haven't found any, short of doing it by hand.  Since my 
>interface specifies a NodeList, which I haven't figured out how to 
>edit, I'm kind of stuck.
>
>Any suggestions?
>
>Thanks.
>
>
>---
>Wayne Johnson, | There are two kinds of people: Those
>3943 Penn Ave. N. | who say to God, "Thy will be done,"
>Minneapolis, MN 55412-1908 | and those to whom God says, "All right,
>(612) 522-7003 | then, have it your way." --C.S. Lewis
>
>
>
>Do you Yahoo!?
><http://vote.yahoo.com>vote.yahoo.com - Register online to vote today!


-- 
Don Bate               | Specializing in Consulting and Mentoring in
Bate Consulting, Inc   | Object-Oriented Technologies,
                        | Software Architecture, and Software Process
(972) 618-0208 voice
(972) 618-0216 fax
donbate@iadfw.net

RE: Searching for elements without decending into children

Posted by "F. Andy Seidl" <fa...@myst-technology.com>.
Wayne,
You can use an XPath expression like "//RuleTemplate/RuleDefinition" to
select RuleDefinition nodes, but only those that have a RuleTemplate parent
node.
Take a look at the CachedXPathAPI or XPathAPI classes and the
selectNodeList() method.

  -- fas

F. Andy Seidl, Co-founder
MyST Technology Partners
Creators of MySmartChannels
 
 
________________________________________
From: Wayne Johnson [mailto:wdtj@yahoo.com] 
Sent: Monday, September 20, 2004 10:17 AM
To: xerces-j-user
Subject: Searching for elements without decending into children

I have an XML structure where a specific element is referenced at two levels
of the same tree:
 
<RuleTemplate>
  <RuleDefinition/>
  <ConcreteRule>
    <RuleDefinition/>
  </ConcreteRule>
</RuleTemplate>
 
I'd like to search for all the RuleDefinitions that are children of
RuleTemplate, but not the one in ConcreteRule.  
 
The XML maps to the object structures so changing things is not an option.  
 
Up till now I was using getElementsByTagNamegetElementsByTagName, but
recently realized it wasn't doing what I thought.  
 
I would have suspected that DOM would have another way to do this, but
haven't found any, short of doing it by hand.  Since my interface specifies
a NodeList, which I haven't figured out how to edit, I'm kind of stuck.
 
Any suggestions?  
 
Thanks.


---
Wayne Johnson, | There are two kinds of people: Those 
3943 Penn Ave. N. | who say to God, "Thy will be done," 
Minneapolis, MN 55412-1908 | and those to whom God says, "All right, 
(612) 522-7003 | then, have it your way." --C.S. Lewis
________________________________________
Do you Yahoo!?
vote.yahoo.com - Register online to vote today!



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org