Author: amassari
Date: Fri Mar 14 08:10:33 2008
New Revision: 637126

URL: http://svn.apache.org/viewvc?rev=637126&view=rev
Log:
DOMPrint now shows how to run an XPath query on a DOM tree (XERCESC-1786)

Modified:
    xerces/c/trunk/samples/src/DOMPrint/DOMPrint.cpp
    xerces/c/trunk/scripts/sanityTest.pl
    xerces/c/trunk/scripts/sanityTest_ExpectedResult.log

Modified: xerces/c/trunk/samples/src/DOMPrint/DOMPrint.cpp
URL: 
http://svn.apache.org/viewvc/xerces/c/trunk/samples/src/DOMPrint/DOMPrint.cpp?rev=637126&r1=637125&r2=637126&view=diff
==============================================================================
--- xerces/c/trunk/samples/src/DOMPrint/DOMPrint.cpp (original)
+++ xerces/c/trunk/samples/src/DOMPrint/DOMPrint.cpp Fri Mar 14 08:10:33 2008
@@ -127,6 +127,8 @@
 static bool                     gDoCreate              = false;
 
 static char*                    goutputfile            = 0;
+static char*                    gXPathExpression       = 0;
+
 // options for DOMLSSerializer's features
 static XMLCh*                   gOutputEncoding        = 0;
 
@@ -169,6 +171,7 @@
             "    -wflt=xxx   Enable/Disable filtering.                 Default 
off\n"
             "    -wfpp=xxx   Enable/Disable format-pretty-print.       Default 
off\n"
             "    -wbom=xxx   Enable/Disable write Byte-Order-Mark      Default 
off\n"
+            "    -xpath=xxx  Prints only the nodes matching the given XPath.\n"
             "    -?          Show this help.\n\n"
             "  * = Default if not provided explicitly.\n\n"
             "The parser has intrinsic support for the following encodings:\n"
@@ -346,6 +349,10 @@
                 return 2;
             }
         }
+         else if (!strncmp(argV[parmInd], "-xpath=", 7))
+        {
+             gXPathExpression = &(argV[parmInd][7]);
+        }
          else
         {
             XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
@@ -487,12 +494,34 @@
             theOutputDesc->setByteStream(myFormTarget);
 
             // get the DOM representation
-            DOMNode                     *doc = parser->getDocument();
+            DOMDocument *doc = parser->getDocument();
 
             //
             // do the serialization through DOMLSSerializer::write();
             //
-            theSerializer->write(doc, theOutputDesc);
+            if(gXPathExpression!=NULL)
+            {
+                XMLCh* xpathStr=XMLString::transcode(gXPathExpression);
+                try
+                {
+                    const DOMXPathNSResolver* 
resolver=doc->createNSResolver(doc->getDocumentElement());
+                    DOMXPathResult* 
result=(DOMXPathResult*)doc->evaluate(xpathStr, doc->getDocumentElement(), 
resolver, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL);
+                    unsigned long nLength=result->getSnapshotLength();
+                    for(unsigned long i=0;i<nLength;i++)
+                        theSerializer->write(result->snapshotItem(i), 
theOutputDesc);
+                    result->release();
+                }
+                catch(const DOMXPathException& e)
+                {
+                    XERCES_STD_QUALIFIER cerr << "An error occurred during 
processing of the XPath expression. Msg is:"
+                        << XERCES_STD_QUALIFIER endl
+                        << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
+                    retval = 4;
+                }
+                XMLString::release(&xpathStr);
+            }
+            else
+                theSerializer->write(doc, theOutputDesc);
 
             theOutputDesc->release();
             theSerializer->release();

Modified: xerces/c/trunk/scripts/sanityTest.pl
URL: 
http://svn.apache.org/viewvc/xerces/c/trunk/scripts/sanityTest.pl?rev=637126&r1=637125&r2=637126&view=diff
==============================================================================
--- xerces/c/trunk/scripts/sanityTest.pl (original)
+++ xerces/c/trunk/scripts/sanityTest.pl Fri Mar 14 08:10:33 2008
@@ -95,6 +95,7 @@
 system ("DOMPrint -wfpp=on -wddc=off personal.xml");
 system ("DOMPrint -wfpp=on -wddc=on  personal.xml");
 system ("DOMPrint -wfpp=on -wddc=off -n -s personal-schema.xml");
+system ("DOMPrint -v=never -xpath=//name personal.xml");
 
 #  Run StdInParse
 system ("StdInParse < personal.xml");

Modified: xerces/c/trunk/scripts/sanityTest_ExpectedResult.log
URL: 
http://svn.apache.org/viewvc/xerces/c/trunk/scripts/sanityTest_ExpectedResult.log?rev=637126&r1=637125&r2=637126&view=diff
==============================================================================
--- xerces/c/trunk/scripts/sanityTest_ExpectedResult.log (original)
+++ xerces/c/trunk/scripts/sanityTest_ExpectedResult.log Fri Mar 14 08:10:33 
2008
@@ -650,6 +650,7 @@
     -wflt=xxx   Enable/Disable filtering.                 Default off
     -wfpp=xxx   Enable/Disable format-pretty-print.       Default off
     -wbom=xxx   Enable/Disable write Byte-Order-Mark      Default off
+    -xpath=xxx  Prints only the nodes matching the given XPath.
     -?          Show this help.
 
   * = Default if not provided explicitly.
@@ -902,7 +903,7 @@
 
 
 </personnel>
-stdin:{timing removed}(37 elems, 12 attrs, 134 spaces, 134 chars)
+<name><family>Boss</family> 
<given>Big</given></name><name><family>Worker</family> 
<given>One</given></name><name><family>Worker</family> 
<given>Two</given></name><name><family>Worker</family> 
<given>Three</given></name><name><family>Worker</family> 
<given>Four</given></name><name><family>Worker</family> 
<given>Five</given></name>stdin:{timing removed}(37 elems, 12 attrs, 134 
spaces, 134 chars)
 stdin:{timing removed}(37 elems, 12 attrs, 0 spaces, 268 chars)
 stdin:{timing removed}(37 elems, 29 attrs, 140 spaces, 128 chars)
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to