R
Ralph Stuber
Hello,
I just started coding in c++, and I am currently working on a library to
access stored data in xml format. I am using VC++6 and xerces-c-2.6.0. I
parsed an xml document via a DOMBuilder*. If I take a DOMNode*, I am
able to access its value by myNode->getFirstChild()->getNodeValue(). The
parsed xml document contains an xml schema definition, which actually
defines two types of elements under the root element:
1) <nominal> with type <xs:string>
2) <numerical> with type <xs:integer>
The method myNode->getFirstChild()->getNodeType() always returns the
value 3 which means that is is of type TEXT_NODE.
My question: is it possible to determine the type of stored data in a
DOMNode? I want to determine if it is a xs:string or an xs:integer. So I
would be able to create char* results for string nodes (f.ex. company
name), and integer results for integer nodes (f.ex. zipcode). Does the
DOMBuilder parse the xml schema and does it transport the information
provided by the scheme into the DOMNode*-pointer?
Thanky in advance,
greetings from Oldenburg, Germany
Ralph
P.S.: Code-Sniplets:
Parsing:
[...]
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
DOMBuilder* parser = ((DOMImplementationLS*)impl)->
createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
if (parser->canSetFeature(XMLUni::fgDOMValidation, true))
parser->setFeature(XMLUni::fgDOMValidation, true);
if (parser->canSetFeature(XMLUni::fgDOMNamespaces, true))
parser->setFeature(XMLUni::fgDOMNamespaces, true);
if (parser->canSetFeature(XMLUni::fgDOMDatatypeNormalization, true))
parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
rootNode = parser->parseURI(xmlFile); // rootNode is a DOMNode*
[...]
Reading and trying to determine the value type...
char* getValue(DOMNode* rootNode, char* elementName) {
char* nodeValue = "";
try{
DOMNodeList* rootElementList = rootNode->getChildNodes();
rootNode = rootElementList->item(0);
DOMNodeList* subElementList = rootNode->getChildNodes();
DOMNode* current = 0;
for (unsigned int i=0; i<subElementList->getLength(); i++) {
current = subElementList->item(i);
if (current->getNodeType() == DOMNode::ELEMENT_NODE) {
char* strValue = XMLString::transcode(current->getNodeName());
if (XMLString::equals(strValue, elementName)) {
nodeValue = XMLString::transcode(current->getFirstChild()->getNodeValue());
cout << "Element-Wert:" << nodeValue << endl;
cout << "Element-Typ:" << current->getFirstChild()->getNodeType() << endl;
//*******************************************************************
// it would be great here to determine the type of the
// current->getFirstChild() value
//*******************************************************************
} else {
}
}
}
} catch (...) {
}
return nodeValue;
}
The xml document:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="carelis_config.xsd">
<nominal>Test eines nominalen Wertes</nominal>
<numerical>12221</numerical>
</config>
The xml schema:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="nominal" type="xs:string"/>
<xs:element name="numerical" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I just started coding in c++, and I am currently working on a library to
access stored data in xml format. I am using VC++6 and xerces-c-2.6.0. I
parsed an xml document via a DOMBuilder*. If I take a DOMNode*, I am
able to access its value by myNode->getFirstChild()->getNodeValue(). The
parsed xml document contains an xml schema definition, which actually
defines two types of elements under the root element:
1) <nominal> with type <xs:string>
2) <numerical> with type <xs:integer>
The method myNode->getFirstChild()->getNodeType() always returns the
value 3 which means that is is of type TEXT_NODE.
My question: is it possible to determine the type of stored data in a
DOMNode? I want to determine if it is a xs:string or an xs:integer. So I
would be able to create char* results for string nodes (f.ex. company
name), and integer results for integer nodes (f.ex. zipcode). Does the
DOMBuilder parse the xml schema and does it transport the information
provided by the scheme into the DOMNode*-pointer?
Thanky in advance,
greetings from Oldenburg, Germany
Ralph
P.S.: Code-Sniplets:
Parsing:
[...]
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
DOMBuilder* parser = ((DOMImplementationLS*)impl)->
createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
if (parser->canSetFeature(XMLUni::fgDOMValidation, true))
parser->setFeature(XMLUni::fgDOMValidation, true);
if (parser->canSetFeature(XMLUni::fgDOMNamespaces, true))
parser->setFeature(XMLUni::fgDOMNamespaces, true);
if (parser->canSetFeature(XMLUni::fgDOMDatatypeNormalization, true))
parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
rootNode = parser->parseURI(xmlFile); // rootNode is a DOMNode*
[...]
Reading and trying to determine the value type...
char* getValue(DOMNode* rootNode, char* elementName) {
char* nodeValue = "";
try{
DOMNodeList* rootElementList = rootNode->getChildNodes();
rootNode = rootElementList->item(0);
DOMNodeList* subElementList = rootNode->getChildNodes();
DOMNode* current = 0;
for (unsigned int i=0; i<subElementList->getLength(); i++) {
current = subElementList->item(i);
if (current->getNodeType() == DOMNode::ELEMENT_NODE) {
char* strValue = XMLString::transcode(current->getNodeName());
if (XMLString::equals(strValue, elementName)) {
nodeValue = XMLString::transcode(current->getFirstChild()->getNodeValue());
cout << "Element-Wert:" << nodeValue << endl;
cout << "Element-Typ:" << current->getFirstChild()->getNodeType() << endl;
//*******************************************************************
// it would be great here to determine the type of the
// current->getFirstChild() value
//*******************************************************************
} else {
}
}
}
} catch (...) {
}
return nodeValue;
}
The xml document:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="carelis_config.xsd">
<nominal>Test eines nominalen Wertes</nominal>
<numerical>12221</numerical>
</config>
The xml schema:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="nominal" type="xs:string"/>
<xs:element name="numerical" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>