You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Kimberly Stewart <Ki...@bsci.com> on 2009/09/26 01:35:44 UTC

ELEMENT_NODE appears as TEXT_NODE

I'm a novice to both XML authoring and Xerces so I'm working from
samples to get everything set up.  I've started a simple program to
parse my sample .xml and output it's contents.
 
I was expecting several ELEMENT_NODEs and a few TEXT_NODEs, however my
output indicates that everything in my .xml is a TEXT_NODE.
 
This is preventing me from being able to access the actual element name,
and is returning #text as the node name for every node in the DOM tree.
 
Any suggestions on why this is occurring and how to correctly identify
the elements as ELEMENT_NODEs?
 
Thanks!
 
Here's the pertinent code:
 
// create tree walker to traverse all elements in the DOMDocument Tree
DOMElement *docRoot = document->getDocumentElement(); // get root to
give walker
DOMTreeWalker* walker = document->createTreeWalker(docRoot,
DOMNodeFilter::SHOW_TEXT, NULL, true);
DOMNode* curNode;
   
// traverse the DOMDocument
while ((curNode = walker->nextNode()) != NULL)
{
    // print out information on the nodes
    char* strValue = XMLString::transcode(curNode->getNodeValue());
    printf("Nodes Value is: %s \n ", strValue);
    XMLString::release(&strValue);

    DOMNode::NodeType type = curNode->getNodeType();
    printf("Node Type: %d \n", type); 
    
    char* strName = XMLString::transcode(curNode->getNodeName());
    printf("Node Name: %s \n", strName);
    XMLString::release(&strName);
    
    char* strContent = XMLString::transcode(curNode->getTextContent()); 
    printf("Node Content: %s \n", strContent);
    XMLString::release(&strContent);
}

And here's the output
 
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
Nodes Value is: 1001
 Node Type: 3
Node Name: #text
Node Content: 1001
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
Nodes Value is: Jag
 Node Type: 3
Node Name: #text
Node Content: Jag
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
Nodes Value is: 27
 Node Type: 3
Node Name: #text
Node Content: 27
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
Nodes Value is:
 
 Node Type: 3
Node Name: #text
Node Content:
 
And here's the .xml
 
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance> "
xsi:noNamespaceSchemaLocation="C:\. . .\Employees.xsd">
 <Employee>
  <ID>1001</ID>
  <Name>Jag</Name>
  <Age>27</Age>
 </Employee> 
</Employees> 
 
And the schema (for good measure)
 
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="Employees">
            <xs:annotation>
                  <xs:documentation>Contains All Employee
information</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="Employee"
maxOccurs="unbounded">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="ID"
type="xs:string"/>
                                          <xs:element name="Name"
type="xs:string"/>
                                          <xs:element name="Age"
type="xs:int"/>
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
      </xs:element> 
</xs:schema>
<ma...@bsci.com> 

Re: help w/ 2.7 XMLString::transcode and release

Posted by Vitaly Prapirny <ma...@mebius.net>.
Michael Case wrote:
> char* temp1 = XMLString::transcode(qname);
> std::string myElementName(temp1);
> XMLString::release(&temp1);
>
> ====
>
> Is the use of XMLString::transcode and XMLString::release correct?

If the constructor of the std::string doesn't throw an exception
than there is no memory leak.

Good luck!
	Vitaly


help w/ 2.7 XMLString::transcode and release

Posted by Michael Case <ca...@physics.ucdavis.edu>.
Hi,

I have the following code which I believe is responsible for a memory 
leak (using valgrind):

void DDLSAX2FileHandler::startElement(const XMLCh* const uri
                                      , const XMLCh* const localname
                                      , const XMLCh* const qname
                                      , const Attributes& attrs)
{

  DCOUT_V('P', "DDLSAX2FileHandler::startElement started");

  char* temp1 = XMLString::transcode(qname);
  std::string 
myElementName(temp1);                                                                                                                                          

  XMLString::release(&temp1);

====

Is the use of XMLString::transcode and XMLString::release correct?

Thank you.

Michael Case


-- 
Michael Case
Software Engineer
High Energy Group

Department of Physics
UC Davis
One Shields Avenue
Davis, CA 95616

Davis: +1-530-754-7226
FNAL: +1-630-840-4626
personal cell: +1-530-902-0594


RE: ELEMENT_NODE appears as TEXT_NODE

Posted by Kimberly Stewart <Ki...@bsci.com>.
Thanks Dave,

This solution gave me the output I was looking for.

~ Kim

-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Friday, September 25, 2009 6:43 PM
To: c-users@xerces.apache.org
Subject: Re: ELEMENT_NODE appears as TEXT_NODE

You're asking the DOMTreeWalker to only show you text nodes, so there's
no way you'll get any element nodes. Of course, you can always call
getParent() on each text node, but perhaps you should instead have the
DOMTreeWalker show you element and text nodes:

DOMTreeWalker* walker =
     document->createTreeWalker(
         docRoot,
         DOMNodeFilter::SHOW_ELEMENT | DOMNodeFilter::SHOW_TEXT,
         NULL,
         true);

Dave

Re: ELEMENT_NODE appears as TEXT_NODE

Posted by David Bertoni <db...@apache.org>.
Kimberly Stewart wrote:
> I'm a novice to both XML authoring and Xerces so I'm working from
> samples to get everything set up.  I've started a simple program to
> parse my sample .xml and output it's contents.
>  
> I was expecting several ELEMENT_NODEs and a few TEXT_NODEs, however my
> output indicates that everything in my .xml is a TEXT_NODE.
This is not possible.

> This is preventing me from being able to access the actual element name,
> and is returning #text as the node name for every node in the DOM tree.
>  
> Any suggestions on why this is occurring and how to correctly identify
> the elements as ELEMENT_NODEs?
Your code must have a bug.

> Thanks!
>  
> Here's the pertinent code:
>  
> // create tree walker to traverse all elements in the DOMDocument Tree
> DOMElement *docRoot = document->getDocumentElement(); // get root to
> give walker
> DOMTreeWalker* walker = document->createTreeWalker(docRoot,
> DOMNodeFilter::SHOW_TEXT, NULL, true);
You're asking the DOMTreeWalker to only show you text nodes, so there's 
no way you'll get any element nodes. Of course, you can always call 
getParent() on each text node, but perhaps you should instead have the 
DOMTreeWalker show you element and text nodes:

DOMTreeWalker* walker =
     document->createTreeWalker(
         docRoot,
         DOMNodeFilter::SHOW_ELEMENT | DOMNodeFilter::SHOW_TEXT,
         NULL,
         true);

Dave