You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by ww...@eecs.umich.edu on 2000/07/19 17:36:21 UTC

Re: TreeWalker

On Mon, 17 Jul 2000 Joseph_Kesselman@lotus.com wrote:

>
> Creating treewalkers using the DOM Level 2 factory methods for doing so,
> rather than by attempting to instantiate them directly, is probably
safer.

    I read the document about DOM level 2 and find that is what I wanted.
Thanks a lot. But I still have some problem about it.

> I'm not sure what that would be in the C++ binding, but in Java it'd be
> something like:
>
>      Document myDoc=current.getOwningDocument(); // if you don't already
> have it handy
>      TreeWalker t=((DocumentTraversal)myDoc).createTreeWalker(
>           current, // root node
>           NodeFilter.SHOW_ALL, // no standard filtering
>           null, // no custom filtering
>           true); // show children of EntityReference nodes 
>
> The C++ API was largely modelled on the Java API, so this should get you
> pointed in the right direction.

     I tried to write it in xerces-c_1.2 but I got some error. First I
can not find class definition for TreeWalker and I dont know how I can
use it in xerces-c_1.2. I tried to use TreeWalker as a type but I am told
that TreeWalker is an undeclared identifier. So could someone tell me how
I can fix this
problem? Thanks a lot.
Best,
Wuwei                                



Re: TreeWalker

Posted by ww...@eecs.umich.edu.
   I am looking forward to a way to visit parts of a tree.
The idea is that for any node which satisfies the following
acceptNode function, I will visit its children nodes.

class Core_class : public DOM_NodeFilter {
public :
	short acceptNode(const DOM_Node & n) const;
    Core_class();
} ; 

Core_class:: Core_class() : DOM_NodeFilter() {}

short Core_class::acceptNode(const DOM_Node &n) const
{
    if(n.getNodeType()==DOM_Node::ELEMENT_NODE) {
	    if(n.getNodeName().equals("Foundation.Core.Class")) {
	        return FILTER_ACCEPT;
            }
	}
	return FILTER_SKIP;
}

  Its children nodes I want to visit is safisfying the following
function:

class StateMachine_simple : public DOM_NodeFilter {
public :
	short acceptNode(const DOM_Node & n) const;
    StateMachine_simple();
} ; 

StateMachine_simple:: StateMachine_simple() : DOM_NodeFilter() {}


short StateMachine_simple::acceptNode(const DOM_Node &n) const
{
 if(n.getNodeType()==DOM_Node::ELEMENT_NODE) {
  if(n.getNodeName().equals("Behavioral_Elements.State_Machines.SimpleState")) {
			return FILTER_ACCEPT;
  }
 }
 return FILTER_SKIP;
}

   The way I try to visit some nodes for a given node is as follows:

void show_textc(DOM_Document root, DOM_Node t, ofstream & outfile)
{
   StateMachine_simple * ptr_c = new StateMachine_simple();
   DOM_TreeWalker c_root = root.createTreeWalker(t,
                                              DOM_NodeFilter::SHOW_ELEMENT,
					      ptr_c,
					      true);
   DOM_Node ele;
   int count = 0;

   ele = c_root.nextNode();

   while(ele!=NULL) {
      // print something for this element;
      ele = c_root.nextNode();
   }
}

void Ele_Print(DOM_Document node)
{
	DOM_NodeList list = node.getElementsByTagName("*");
        Core_class * new_p = new Core_class();
        DOM_Node current = list.item(0), ele_node;
	DOM_TreeWalker t_root = node.createTreeWalker(
		                      current,
                              DOM_NodeFilter::SHOW_ALL, 
                              new_p,                     
                              true);
	ele_node = t_root.nextNode();

	while(ele_node!=NULL) {
           // print sth for this node;
           show_textc(node, ele_node, outfile);
	   ele_node = t_root.nextSibling();
	   count++;
	}
}

   The problem is that when the xml file is done, the program is dead.
when I change ele_node = t_root.nextSibling() to ele_node =
t_root.nextNode(), the program enters the infinite loop after
finishing reading the file. Could someone tell me how I can 
visit the parts of a tree and fix this problem. Thanks a lot
in advance!!!
best,
wuwei


Re: TreeWalker

Posted by Joe Polastre <jp...@apache.org>.
All dom related classes that you need start with DOM_

Please check out ODM_TreeWalker.

All of the info you need is in the API docs online
xml.apache.org/xerces-c/

I would *highly* recommend reading the API docs as most of this information
(and much more) is contained there.

The C version works just like the java code below with a few modifications:

DOM_Document myDoc=current.getOwnerDocument(); // if you don't alreadyhave
it handy
DOM_TreeWalker t = myDoc.createTreeWalker(
           current,                  // root node
           DOM_NodeFilter::SHOW_ALL, // no standard filtering
           null,                     // no custom filtering
           true);                    // show children of EntityReference
nodes


-Joe Polastre  (jpolast@apache.org)
IBM Cupertino, XML Technology Group


----- Original Message -----
From: <ww...@eecs.umich.edu>
To: <xe...@xml.apache.org>
Sent: Wednesday, July 19, 2000 8:36 AM
Subject: Re: TreeWalker


> On Mon, 17 Jul 2000 Joseph_Kesselman@lotus.com wrote:
>
> >
> > Creating treewalkers using the DOM Level 2 factory methods for doing so,
> > rather than by attempting to instantiate them directly, is probably
> safer.
>
>     I read the document about DOM level 2 and find that is what I wanted.
> Thanks a lot. But I still have some problem about it.
>
> > I'm not sure what that would be in the C++ binding, but in Java it'd be
> > something like:
> >
> >      Document myDoc=current.getOwningDocument(); // if you don't already
> > have it handy
> >      TreeWalker t=((DocumentTraversal)myDoc).createTreeWalker(
> >           current, // root node
> >           NodeFilter.SHOW_ALL, // no standard filtering
> >           null, // no custom filtering
> >           true); // show children of EntityReference nodes
> >
> > The C++ API was largely modelled on the Java API, so this should get you
> > pointed in the right direction.
>
>      I tried to write it in xerces-c_1.2 but I got some error. First I
> can not find class definition for TreeWalker and I dont know how I can
> use it in xerces-c_1.2. I tried to use TreeWalker as a type but I am told
> that TreeWalker is an undeclared identifier. So could someone tell me how
> I can fix this
> problem? Thanks a lot.
> Best,
> Wuwei
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>