Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Querying an XMLType with the C++ XDK APIs

At this point, the C++ XML functions can operate on the document node in exactly the same way as if it were parsed from a file. These APIs are identical and thus your code can be portable for the actual application logic you need to perform. The following generic template, doXPath(), evaluates the XPath expression, doXPath, and prints out the result to the screen:

 /* generic template to handle XPath processing */ 
template< typename TCtx, typename Tnode>
sb4 doXPath( TCtx* ctxp, DocumentRef< Tnode>& doc_ref, char* xpath_exp) {
printf("Initializing Tools Factory\n");
Factory< TCtx, Tnode>* fp = NULL;
try {
fp = new Factory< TCtx, Tnode>( ctxp); }
catch (FactoryException& fe) {
unsigned ecode = fe.getCode();
printf( "Failed to create factory, error %u\n", ecode);
return ecode;}
printf("Creating XPath processor\n");
XPath::Processor< TCtx, Tnode>* prp = NULL;
try {
prp = fp->createXPathProcessor( XPathPrCXml, NULL);}
catch (FactoryException& fe1) {
unsigned ecode = fe1.getCode();
printf( "Failed to create XPath processor, error %u\n", ecode);

return ecode; }
printf( "Create DOM source for the DOM tree\n");
InputSource* isrcp = new DOMSource< Tnode>( doc_ref);
printf("Processing DOM tree with '%s' ...\n", xpath_exp);
XPathObject< Tnode>* objp = NULL;
try {
objp = prp->process (isrcp, (oratext*)xpath_exp); }
catch (XPathException& xpe) {
unsigned ecode = xpe.getCode();
printf( "Failed to process the document, error %u\n", ecode);
return ecode; }
NodeSet< Tnode>* np = NULL;
boolean varb = FALSE;
double num = 0.0;
oratext* str = NULL;
unsigned i = 0;
switch (objp->getObjType()) {
case XPOBJ_TYPE_NDSET:
np = objp->getNodeSet();
printf("NodeSet:\n");
for (i = 0; i < np->getSize(); i++ ) {
NodeRef< Tnode>* nrefp = np->getNode( i);
switch( nrefp->getNodeType()) {
case ELEMENT_NODE:
case ATTRIBUTE_NODE:
printf("Node Name : %s\n",
nrefp->getNodeName());
break;
default:
printf("Other Node: %s\n" nrefp->getNodeValue() );
break; }
}
break;
case XPOBJ_TYPE_BOOL:
varb = objp->getObjBoolean();
printf("Boolean Value : %d\n", varb);
break;
case XPOBJ_TYPE_NUM:
num = objp->getObjNumber();
printf("Numeric Value : %10.2f\n", num);
break;
case XPOBJ_TYPE_STR:
str = objp->getObjString();
printf("String Value : %s\n", str);
break;
default:
printf( "Failed to create valid object\n");
}
return 0;
}


This code has several sections. First, you must create an XPath processor, which can accept as parameters your XPath expression and database DOM tree and then return the result. Since the result may be a collection of nodes, a string, a number, or a Boolean, you need to set up a case statement because the code to print these items is not the same.

For a node set, you need to iterate over each item and properly detect whether they are a set of attributes or elements. You must convert Boolean and number values to strings before you can print them. Even though they started out as lexical representations, the XPath processor implicitly cast them per the 1.0 specification.

/ 218