/* Some useful utilities that work with the Xerces-C API. Allows you to
* get the actual data out of Elements and Attributes. Uses DualString
* class which can be down loaded from the O'Reilly tutorial:
* http://www.onlamp.com/pub/a/onlamp/2005/09/08/xerces_dom.html
* Code below is copyright Alisa Neeman Feb. 4, 2006
* COPY LEFT and RIGHT:
* Commercial use of this program without express permission of
* the University, is strictly encouraged. Copyright and Copyleft are covered
* by the following clause:
*
* Woody's (Guthrie) license:
* ``This source code is Copyrighted in U.S., by the The University
* and by the Author, for an indefinite period, and anybody caught
* using it without our permission, will be mighty good friends of
* ourn, cause we don't give a darn. Hack it. Compile it. Debug it. Run it.
* Yodel it. Enjoy it. We wrote it, that's all we wanted to do.'' bj
*/
/*
* given a Document Element, and an attribute name, return the value
* of the attribute if it is an integer
*/
int getAttrIntValueByName( DOMElement * el, const XMLCh * attrName ) {
int id;
DOMAttr * idVal = el->getAttributeNode( attrName );
DualString valString( idVal->getValue() ); // no worries: string is copied
id = atoi( valString.asCString() );
// DualString valString destructor called on return
return id;
}
/**
* given a Document Element, and an attribute name, return the value
* of the attribute if it is an double
*/
double getAttrDoubleValueByName( DOMElement * el, const XMLCh * attrName ) {
double val ;
DOMAttr * attVal = el->getAttributeNode( attrName );
DualString valString( attVal->getValue() );
val = atof( valString.asCString() );
return val;
}
/**
* Given an document element, and the name of one of its
* child elements, fetch the first child so named and return
* the value, if the value is an integer.
* integer
*/
int getFirstChildIntValue( DOMElement * el, const XMLCh * childName ) {
DOMNodeList * nl;
int val = -1;
nl = el->getElementsByTagName( childName );
if( nl != NULL && nl->getLength() > 0 ) {
DOMElement * kinder =
dynamic_cast ( nl->item(0));
if ( kinder == NULL ) {
std::cerr << "Error trying to get node's " <<
XMLString::transcode(childName);
exit (-1);
}
//try getting child
//this should be a DOMCaharacterData node
DOMCharacterData * chardata =
dynamic_cast ( kinder->getFirstChild() );
DualString data( chardata->getData() );
val = atoi( data.asCString() );
}
return val;
}
/**
* Given an document element, and the name of one of its
* child elements, fetch the first child so named and return
* the value, if the value is a double.
* double
*/
double getFirstChildDoubleValue( DOMElement * el, const
XMLCh * childName ) {
DOMNodeList * nl;
double val = -1;
nl = el->getElementsByTagName( childName );
if( nl != NULL && nl->getLength() > 0 ) {
DOMElement * kinder = dynamic_cast ( nl->item(0) );
if ( kinder == NULL ) {
std::cerr << "Error trying to get node's " <<
XMLString::transcode( childName );
exit (-1);
}
//try getting child
//this should be a DOMCharacterData node
DOMCharacterData * chardata =
dynamic_cast ( kinder->getFirstChild() );
DualString data( chardata->getData() );
val = atof( data.asCString() );
}
return val;
}