xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tinny Ng" <tng-...@ca.ibm.com>
Subject Re: TreeWalker not working
Date Thu, 08 Aug 2002 13:28:30 GMT
Craig,

>   // Find the root element
>   //
>   const XMLCh* xmls = doc->getFirstChild()->getNodeName();

If the DOCTYPE exist, then the getFirstChild will return the doctype node,
not the root element.   Please use getDocumentElement() to retrieve the root
element.

Tinny

----- Original Message -----
From: "Craig L. Ching" <cching@mqsoftware.com>
To: <general@xml.apache.org>
Sent: Wednesday, August 07, 2002 5:49 PM
Subject: TreeWalker not working


I tried posting this in the general list and got redirected here.  Also, for
the code below, if I turn of validation and remove the dtd line from my xml
file (have to do both), the code works.  Is this a defect?  I appreciate any
help/advice!

Cheers,
Craig

> Hi all,
>
> I've searched through the archives and couldn't find an
> answer to my question.  I'm working with xerces-c 2.0.0
> (Windows 2k) just downloaded today (trying to move some code
> from 1.2).  I've kludged together some code to illustrate the
> problem I'm having.  Basically, I'm trying to walk this xml file:
>
> <?xml version="1.0"?>
> <!DOCTYPE Settings SYSTEM "Test.dtd">
> <Settings>
> <SettingsVersion>1.0</SettingsVersion>
> <Options>
>     <DSN>
>         <Name>mydsn</Name>
>         <User>myuser</User>
>         <Password>mypasswd</Password>
>     </DSN>
> </Options>
> </Settings>
>
> With this dtd:
>
> <!ELEMENT Settings (SettingsVersion, Options)>
> <!ELEMENT SettingsVersion (#PCDATA)>
> <!ELEMENT Options (DSN)>
> <!ELEMENT DSN (Name, User, Password)>
> <!ELEMENT Name (#PCDATA)>
> <!ELEMENT User (#PCDATA)>
> <!ELEMENT Password (#PCDATA)>
>
> With this code:
>
> #include <xercesc/parsers/XercesDOMParser.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> #include <xercesc/dom/DOM.hpp>
> #include <xercesc/util/XMLUniDefs.hpp>
>
> #include "DOMTreeErrorReporter.h"
>
> #include <iostream>
>
> class  MyFilter : public DOMNodeFilter {
> public:
>
>   MyFilter(short nodeType, bool reject=false) :
> DOMNodeFilter(), fNodeType(nodeType), fReject(reject) {};
>   virtual short acceptNode(const DOMNode* node) const;
> private:
>     short fNodeType;
>     bool fReject;
> };
> /*
>         Node Types can be of the following:
>         ELEMENT_NODE         = 1,
>         ATTRIBUTE_NODE       = 2,
>         TEXT_NODE            = 3,
>         CDATA_SECTION_NODE   = 4,
>         ENTITY_REFERENCE_NODE = 5,
>         ENTITY_NODE          = 6,
>         PROCESSING_INSTRUCTION_NODE = 7,
>         COMMENT_NODE         = 8,
>         DOCUMENT_NODE        = 9,
>         DOCUMENT_TYPE_NODE   = 10,
>         DOCUMENT_FRAGMENT_NODE = 11,
>         NOTATION_NODE        = 12
> */
> short  MyFilter::acceptNode(const DOMNode* node) const {
>     if (fNodeType == 0)
>         return  DOMNodeFilter::FILTER_ACCEPT;
> if (node->getNodeType() ==  fNodeType) {
>        return  DOMNodeFilter::FILTER_ACCEPT;
> } else {
>     return  fReject ? DOMNodeFilter::FILTER_REJECT :
> DOMNodeFilter::FILTER_SKIP;
> }
> }
>
> int main (int argc, char* argv [])
> {
>   // Initialize the Xerces-C system
>   try
>   {
>     XMLPlatformUtils::Initialize();
>   }
>
>   catch(const XMLException&)
>   {
>     std::cerr << "Error initializing Xerces" << std::endl;
>     return -1;
>   }
>
>   //  Create our parser, then attach an error handler to the parser.
>   //  The parser will call back to methods of the ErrorHandler if it
>   //  discovers errors during the course of parsing the XML document.
>   //
>   XercesDOMParser* parser_ = new XercesDOMParser;
>
>   // We should always have a dtd
>   //
>   parser_->setValidationScheme(XercesDOMParser::Val_Always);
>   parser_->setDoNamespaces(false);
>   DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
>   parser_->setErrorHandler(errReporter);
>   parser_->setExpandEntityReferences(false);
>
>   //  Parse the XML file, catching any XML exceptions that
> might propogate
>   //  out of it.
>   //
>   try
>   {
>     parser_->parse("Test.xml");
>     std::cout << "Parsed successfully" << std::endl;
>   }
>
>   catch(const XMLException&)
>   {
>     std::cerr << "Failed to parse document" << std::endl;
>     return -1;
>   }
>
>   std::string path("Settings.Options.DSN.Name");
>   int pos_beg = 0;
>   int pos_end = path.find(".");
>   std::string elemToFind = path.substr(pos_beg, pos_end);
>
>   DOMDocument* doc = parser_->getDocument();
>
>   // Find the root element
>   //
>   const XMLCh* xmls = doc->getFirstChild()->getNodeName();
>   char* s = XMLString::transcode (xmls);
>   std::string value = s;
>   delete [] s;
>   std::cout << "Value is " << value.c_str() << std::endl;
>
>   MyFilter* filter = new MyFilter(0);
>   DOMNodeIterator*  iter =
> doc->createNodeIterator(doc->getFirstChild(),
> DOMNodeFilter::SHOW_ALL, filter, true);
>   DOMTreeWalker* walker =
> doc->createTreeWalker(iter->nextNode(),
> DOMNodeFilter::SHOW_ALL, /* filter */ 0, true);
>   DOMNode* node = walker->getCurrentNode();
>   while( node )
>   {
>     const XMLCh* nodeName = node->getNodeName();
>     char* chNodeName = XMLString::transcode (nodeName);
>     if( strcmp(chNodeName, elemToFind.c_str()) == 0 )
>     {
>       if( pos_end == -1 ) // We're done!
>         break;
>       node = walker->firstChild(); ///*****
> Always returns null!
>       //            node = walker->nextSibling();
>       pos_beg = pos_end + 1;
>       pos_end = path.find(".", pos_beg);
>       elemToFind = path.substr(pos_beg, pos_end - pos_beg);
>     }
>     else
>     {
>     node = walker->nextSibling();
>     }
>     delete [] chNodeName;
>   }
>
>   delete parser_;
>   XMLPlatformUtils::Terminate();
>   return 0;
> }
>
> Basically, given the string "Settings.Options.DSN.Name", walk
> the tree to the name tag and return me the value.  I can't
> seem to get past the "Settings" node though (refer to the
> line:       node = walker->firstChild();
> ///*****  Always returns null!).  Anyone see what I'm doing wrong?
>
> Cheers,
> Craig
>
> BTW, DOMTreeErrorReporter is from the DOMPrint example.  I
> think it's verbatim, let me know if you need that code from me.
>

---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org



---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Mime
View raw message