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 Ken Miller <ke...@nifics.com> on 2002/10/01 06:16:32 UTC

Help with getting the next sibling

I am creating a linked list when parsing through a DOM document and
having a small problem.  When the node is an element node, I check to
see if there are more children or siblings and then handle the linked
list accordingly.  The problem is that it seems that every node is
treated as a sibling, so a dummy sibling node is created after each
node.  When an actual child is found, it is placed under the dummy
sibling node instead of the actual node.  For example, I get the
following when reading an XML file that has a node called 'model' and a
child  called 'scale':
 
Name:  model
Value:
Name:  *
Value: *
   Name:  scale
   Value: 0.025
 
This shows the dummy sibling (marked with the *) and the scale child
node added under the dummy node vice the 'model' node.  The * in the
name and value is just what is put into those fields when the new linked
list node is created and is not added from the XML file.
 
Any ideas on what I am doing wrong?  I have included the code snippet
below.
 
Thanks.
 
Ken
 
part of a buildList function:
 
case DOM_Node::ELEMENT_NODE :
{
    strcpy(currentPtr->elementName, nodeName.transcode());
 
    // Output any attributes on this element
    DOM_NamedNodeMap attributes = toWrite.getAttributes();
    int attrCount = attributes.getLength();
    for (int i = 0; i < attrCount; i++)
    {
        DOM_Node  attribute = attributes.item(i);
    }
   
   DOM_Node child = toWrite.getFirstChild();
   if (child != 0)
   {
       while( child != 0)
       {
           DOMString tmpName  = child.getNodeName();
           if(child.getNodeType() != DOM_Node::TEXT_NODE) {
               addChildToNode(currentPtr);
               buildList(child, currentPtr->child);
           }
           else {
               buildList(child, currentPtr);
           }
 
          child = child.getNextSibling();
          if(child != 0) {
              DOMString tmpName  = child.getNodeName();
              if(strcmp(tmpName.transcode(), "#text") != 0) {
                  addSiblingToNode(currentPtr);
                  currentPtr = currentPtr->rSibling;
              }
                  
           }
       }
    
     }
    break;
  }


RE: Help with getting the next sibling

Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
	what are you actually trying to do? If you want to just iterate 
over Elements then use a DOMTreeWalker or a DOMNodeIterator with the 
whatToShow parameter set correctly.

Gareth 


On Tue, 1 Oct 2002, Ken Miller wrote:

> Thanks for the replies.  Couple of questions about the whitespace issue:
> 
> Is it possible to tell the parser to ignore whitespaces without the DTD?
> 
> I am using an XSD for the XML file, you can't set ignore whitespaces
> inside the XSD can you?
> 
> Is there anyway to trap the whitespaces inside a program using xerces
> and then just simply not create the dummy sibling if it is a whitespace,
> vice an actual node?  I have tried testing to see if the
> *(nodeValue.transcode()) != '\n' and also to ' ', but no dice.
> 
> Thanks in advance.
> 
> Ken
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 
> 

-- 
Gareth Reakes, Head of Product Development  
DecisionSoft Ltd.            http://www.decisionsoft.com
Office: +44 (0) 1865 203192



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


RE: Help with getting the next sibling

Posted by Ken Miller <ke...@nifics.com>.
Thanks for the replies.  Couple of questions about the whitespace issue:

Is it possible to tell the parser to ignore whitespaces without the DTD?

I am using an XSD for the XML file, you can't set ignore whitespaces
inside the XSD can you?

Is there anyway to trap the whitespaces inside a program using xerces
and then just simply not create the dummy sibling if it is a whitespace,
vice an actual node?  I have tried testing to see if the
*(nodeValue.transcode()) != '\n' and also to ' ', but no dice.

Thanks in advance.

Ken



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


RE: Help with getting the next sibling

Posted by Mark Weaver <ma...@npsl.co.uk>.
I believe it is also possible to provide a DTD and set ignorable whitespace
to true.

Mark

> -----Original Message-----
> From: Gareth Reakes [mailto:gareth@decisionsoft.com]
> Sent: 01 October 2002 08:50
> To: xerces-c-dev@xml.apache.org
> Subject: Re: Help with getting the next sibling
>
>
> Hi,
> 	If speed is a concern then you could derive your own parser and
> override docCharacters so the text nodes you dont want are not ever
> created.
>
> Gareth
>
>
> On 1 Oct 2002, Justin Kirby wrote:
>
> > Xerces, by default, and I know of no way to turn this off if you don't
> > have a dtd, inserts whitespace into the DOM tree as text nodes.
> > So it looks to me that you have a file with the contents:
> > <model>
> >   <scale>0.025</scale>
> > </model>
> >
> >
> > I would recommend adjusting the contents to not have any whitespace.
> > e.g.:
> > <model><scale>0.025</scale></model>
> >
> > Or the other alternative is to implement a cleanup function that walks
> > the entire tree and removes nodes that are nothing but whitespace. I
> > have a bunch of experimental code that happens to do this:
> > http://www.openaether.org/src/source/framework/xmlutil.cpp#L135
> > Feel free to steal it, or improve it... if you do let me know ;)
> >
> >
> >
> > On Tue, 2002-10-01 at 00:16, Ken Miller wrote:
> > > I am creating a linked list when parsing through a DOM document and
> > > having a small problem.  When the node is an element node, I check to
> > > see if there are more children or siblings and then handle the linked
> > > list accordingly.  The problem is that it seems that every node is
> > > treated as a sibling, so a dummy sibling node is created after each
> > > node.  When an actual child is found, it is placed under the dummy
> > > sibling node instead of the actual node.  For example, I get the
> > > following when reading an XML file that has a node called
> 'model' and a
> > > child  called 'scale':
> > >
> > > Name:  model
> > > Value:
> > > Name:  *
> > > Value: *
> > >    Name:  scale
> > >    Value: 0.025
> > >
> > > This shows the dummy sibling (marked with the *) and the scale child
> > > node added under the dummy node vice the 'model' node.  The * in the
> > > name and value is just what is put into those fields when the
> new linked
> > > list node is created and is not added from the XML file.
> > >
> > > Any ideas on what I am doing wrong?  I have included the code snippet
> > > below.
> > >
> > > Thanks.
> > >
> > > Ken
> > >
> > > part of a buildList function:
> > >
> > > case DOM_Node::ELEMENT_NODE :
> > > {
> > >     strcpy(currentPtr->elementName, nodeName.transcode());
> > >
> > >     // Output any attributes on this element
> > >     DOM_NamedNodeMap attributes = toWrite.getAttributes();
> > >     int attrCount = attributes.getLength();
> > >     for (int i = 0; i < attrCount; i++)
> > >     {
> > >         DOM_Node  attribute = attributes.item(i);
> > >     }
> > >
> > >    DOM_Node child = toWrite.getFirstChild();
> > >    if (child != 0)
> > >    {
> > >        while( child != 0)
> > >        {
> > >            DOMString tmpName  = child.getNodeName();
> > >            if(child.getNodeType() != DOM_Node::TEXT_NODE) {
> > >                addChildToNode(currentPtr);
> > >                buildList(child, currentPtr->child);
> > >            }
> > >            else {
> > >                buildList(child, currentPtr);
> > >            }
> > >
> > >           child = child.getNextSibling();
> > >           if(child != 0) {
> > >               DOMString tmpName  = child.getNodeName();
> > >               if(strcmp(tmpName.transcode(), "#text") != 0) {
> > >                   addSiblingToNode(currentPtr);
> > >                   currentPtr = currentPtr->rSibling;
> > >               }
> > >
> > >            }
> > >        }
> > >
> > >      }
> > >     break;
> > >   }
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> >
> >
>
> --
> Gareth Reakes, Head of Product Development
> DecisionSoft Ltd.            http://www.decisionsoft.com
> Office: +44 (0) 1865 203192
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>


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


Re: Help with getting the next sibling

Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
	If speed is a concern then you could derive your own parser and 
override docCharacters so the text nodes you dont want are not ever 
created.

Gareth


On 1 Oct 2002, Justin Kirby wrote:

> Xerces, by default, and I know of no way to turn this off if you don't
> have a dtd, inserts whitespace into the DOM tree as text nodes.
> So it looks to me that you have a file with the contents:
> <model>
>   <scale>0.025</scale>
> </model>
> 
> 
> I would recommend adjusting the contents to not have any whitespace.
> e.g.:
> <model><scale>0.025</scale></model>
> 
> Or the other alternative is to implement a cleanup function that walks
> the entire tree and removes nodes that are nothing but whitespace. I
> have a bunch of experimental code that happens to do this:
> http://www.openaether.org/src/source/framework/xmlutil.cpp#L135
> Feel free to steal it, or improve it... if you do let me know ;)
> 
> 
> 
> On Tue, 2002-10-01 at 00:16, Ken Miller wrote:
> > I am creating a linked list when parsing through a DOM document and
> > having a small problem.  When the node is an element node, I check to
> > see if there are more children or siblings and then handle the linked
> > list accordingly.  The problem is that it seems that every node is
> > treated as a sibling, so a dummy sibling node is created after each
> > node.  When an actual child is found, it is placed under the dummy
> > sibling node instead of the actual node.  For example, I get the
> > following when reading an XML file that has a node called 'model' and a
> > child  called 'scale':
> >  
> > Name:  model
> > Value:
> > Name:  *
> > Value: *
> >    Name:  scale
> >    Value: 0.025
> >  
> > This shows the dummy sibling (marked with the *) and the scale child
> > node added under the dummy node vice the 'model' node.  The * in the
> > name and value is just what is put into those fields when the new linked
> > list node is created and is not added from the XML file.
> >  
> > Any ideas on what I am doing wrong?  I have included the code snippet
> > below.
> >  
> > Thanks.
> >  
> > Ken
> >  
> > part of a buildList function:
> >  
> > case DOM_Node::ELEMENT_NODE :
> > {
> >     strcpy(currentPtr->elementName, nodeName.transcode());
> >  
> >     // Output any attributes on this element
> >     DOM_NamedNodeMap attributes = toWrite.getAttributes();
> >     int attrCount = attributes.getLength();
> >     for (int i = 0; i < attrCount; i++)
> >     {
> >         DOM_Node  attribute = attributes.item(i);
> >     }
> >    
> >    DOM_Node child = toWrite.getFirstChild();
> >    if (child != 0)
> >    {
> >        while( child != 0)
> >        {
> >            DOMString tmpName  = child.getNodeName();
> >            if(child.getNodeType() != DOM_Node::TEXT_NODE) {
> >                addChildToNode(currentPtr);
> >                buildList(child, currentPtr->child);
> >            }
> >            else {
> >                buildList(child, currentPtr);
> >            }
> >  
> >           child = child.getNextSibling();
> >           if(child != 0) {
> >               DOMString tmpName  = child.getNodeName();
> >               if(strcmp(tmpName.transcode(), "#text") != 0) {
> >                   addSiblingToNode(currentPtr);
> >                   currentPtr = currentPtr->rSibling;
> >               }
> >                   
> >            }
> >        }
> >     
> >      }
> >     break;
> >   }
> > 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 
> 

-- 
Gareth Reakes, Head of Product Development  
DecisionSoft Ltd.            http://www.decisionsoft.com
Office: +44 (0) 1865 203192



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


Re: Help with getting the next sibling

Posted by Justin Kirby <ju...@openaether.org>.
Xerces, by default, and I know of no way to turn this off if you don't
have a dtd, inserts whitespace into the DOM tree as text nodes.
So it looks to me that you have a file with the contents:
<model>
  <scale>0.025</scale>
</model>


I would recommend adjusting the contents to not have any whitespace.
e.g.:
<model><scale>0.025</scale></model>

Or the other alternative is to implement a cleanup function that walks
the entire tree and removes nodes that are nothing but whitespace. I
have a bunch of experimental code that happens to do this:
http://www.openaether.org/src/source/framework/xmlutil.cpp#L135
Feel free to steal it, or improve it... if you do let me know ;)



On Tue, 2002-10-01 at 00:16, Ken Miller wrote:
> I am creating a linked list when parsing through a DOM document and
> having a small problem.  When the node is an element node, I check to
> see if there are more children or siblings and then handle the linked
> list accordingly.  The problem is that it seems that every node is
> treated as a sibling, so a dummy sibling node is created after each
> node.  When an actual child is found, it is placed under the dummy
> sibling node instead of the actual node.  For example, I get the
> following when reading an XML file that has a node called 'model' and a
> child  called 'scale':
>  
> Name:  model
> Value:
> Name:  *
> Value: *
>    Name:  scale
>    Value: 0.025
>  
> This shows the dummy sibling (marked with the *) and the scale child
> node added under the dummy node vice the 'model' node.  The * in the
> name and value is just what is put into those fields when the new linked
> list node is created and is not added from the XML file.
>  
> Any ideas on what I am doing wrong?  I have included the code snippet
> below.
>  
> Thanks.
>  
> Ken
>  
> part of a buildList function:
>  
> case DOM_Node::ELEMENT_NODE :
> {
>     strcpy(currentPtr->elementName, nodeName.transcode());
>  
>     // Output any attributes on this element
>     DOM_NamedNodeMap attributes = toWrite.getAttributes();
>     int attrCount = attributes.getLength();
>     for (int i = 0; i < attrCount; i++)
>     {
>         DOM_Node  attribute = attributes.item(i);
>     }
>    
>    DOM_Node child = toWrite.getFirstChild();
>    if (child != 0)
>    {
>        while( child != 0)
>        {
>            DOMString tmpName  = child.getNodeName();
>            if(child.getNodeType() != DOM_Node::TEXT_NODE) {
>                addChildToNode(currentPtr);
>                buildList(child, currentPtr->child);
>            }
>            else {
>                buildList(child, currentPtr);
>            }
>  
>           child = child.getNextSibling();
>           if(child != 0) {
>               DOMString tmpName  = child.getNodeName();
>               if(strcmp(tmpName.transcode(), "#text") != 0) {
>                   addSiblingToNode(currentPtr);
>                   currentPtr = currentPtr->rSibling;
>               }
>                   
>            }
>        }
>     
>      }
>     break;
>   }
> 



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