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/25 21:49:58 UTC

how can I use acceptNode in creatTreeWalker

   I have a problem in visiting some nodes in  the document
tree. I am not sure whether the following way to use the
NodeFilter's acceptNode is true or not. The problem is
that when I run the my example, the program runs infinitely
and no response. But when I change XMI.extender to person
to run the example personal.xml, it works. Could someone
tell me why I can not run my example. Enclosed is my
xml file example. I include the following program in 
domcount.cpp file. Thanks a lot if someone can help me
to take a look at this.
Best,
Wuwei

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("XMI.header")) {
//          if(n.getNodeName().equals("person")) {
			return DOM_NodeFilter::FILTER_ACCEPT;
		}
	}
	return DOM_NodeFilter::FILTER_SKIP;
}

void Ele_Print(DOM_Document node)
{
	DOM_Element first = node.getDocumentElement();
	ofstream outfile;
	int count = 0;
    Core_class * new_p = new Core_class();
    DOM_Node current = (DOM_Node) first, ele_node;
	DOM_TreeWalker t_root = node.createTreeWalker(
		                      current,
                              DOM_NodeFilter::SHOW_ELEMENT, 
                              new_p,          
                              true);

    outfile.open("temp.txt",ios::out);
	ele_node = t_root.nextNode(); 

	while(ele_node!=NULL) {
           outfile << count << "\telement is\t";
	       outfile << ele_node.getNodeName().transcode() << endl;
	       count++;
           ele_node = t_root.nextNode();
	}
}

Re: how can I use acceptNode in creatTreeWalker

Posted by ww...@eecs.umich.edu.
On Tue, 25 Jul 2000, Arundhati Bhowmick wrote:

> The usage looks fine to me. Now if your xml files does not have elements by
> the name XMI.header it will skip. So, first try to accept all nodes and see if
> your application works on it.

   Yes, the input file phi.xml includes that element and it is output
to the file temp.txt according to the program in my previous email. I
also change the whatToShow to DOM_NodeFilter::SHOW_ALL and get the same
result. I really can not understand the reason. But everything will
disappear when I use NULL, indicating no filter, in createTreeWalker.
I highly appreciate for the reason causing my problem.
Best,
Wuwei


Re: how can I use acceptNode in creatTreeWalker

Posted by Arundhati Bhowmick <ar...@hyperreal.org>.
The usage looks fine to me. Now if your xml files does not have elements by
the name XMI.header it will skip. So, first try to accept all nodes and see if
your application works on it.
--

Arundhati Bhowmick
IBM -- XML Technology Group (Silicon Valley)


wwshen@eecs.umich.edu wrote:

>    I have a problem in visiting some nodes in  the document
> tree. I am not sure whether the following way to use the
> NodeFilter's acceptNode is true or not. The problem is
> that when I run the my example, the program runs infinitely
> and no response. But when I change XMI.extender to person
> to run the example personal.xml, it works. Could someone
> tell me why I can not run my example. Enclosed is my
> xml file example. I include the following program in
> domcount.cpp file. Thanks a lot if someone can help me
> to take a look at this.
> Best,
> Wuwei
>
> 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("XMI.header")) {
> //          if(n.getNodeName().equals("person")) {
>                         return DOM_NodeFilter::FILTER_ACCEPT;
>                 }
>         }
>         return DOM_NodeFilter::FILTER_SKIP;
> }
>
> void Ele_Print(DOM_Document node)
> {
>         DOM_Element first = node.getDocumentElement();
>         ofstream outfile;
>         int count = 0;
>     Core_class * new_p = new Core_class();
>     DOM_Node current = (DOM_Node) first, ele_node;
>         DOM_TreeWalker t_root = node.createTreeWalker(
>                                       current,
>                               DOM_NodeFilter::SHOW_ELEMENT,
>                               new_p,
>                               true);
>
>     outfile.open("temp.txt",ios::out);
>         ele_node = t_root.nextNode();
>
>         while(ele_node!=NULL) {
>            outfile << count << "\telement is\t";
>                outfile << ele_node.getNodeName().transcode() << endl;
>                count++;
>            ele_node = t_root.nextNode();
>         }
> }
>
>   ------------------------------------------------------------------------
>               Name: phi.xml
>    phi.xml    Type: Plain Text (TEXT/PLAIN)
>           Encoding: BASE64
>
>   ------------------------------------------------------------------------
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org