Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: How do I get the value out of an IXMLDOMNodePtr? [Thread Next] Re: How do I get the value out of an IXMLDOMNodePtr?To: NULL Date: 11/7/2008 12:26:00 PM
"Christopher" <cpisz@a...> wrote in message
news:ff5cc47e-3ae0-45f7-8605-2e3dfaf7bd8e@p......
> On Nov 2, 5:29 am, "Joe Fawcett" <joefawc...@newsgroup.nospam> wrote:
>> "Christopher" <cp...@austin.rr.com> wrote in message
>>
>> news:59d371a1-dfcc-44a5-b1c4-ce0689c1934f@p......
>>
>>
>>
>> > MSDN's examples only seem to show how to get the xml string out of a
>> > node.
>> > I don't want the xml string! That's why I'm using your XML library in
>> > the first place sillies!
>>
>> > Anyway, I've been pooring over the documentation for hours and I can't
>> > seem to come up with a way to get my value out with anything but
>> > GetText, which seems to come back with all child node values as well.
>> > I just want the one single value, preferably as an unsigned int.
>>
>> > xml:
>> > <economy>
>>
>> > <good>
>> > <name>Energy</name>
>> > <value>12</value>
>> > <tech>1</tech>
>> > </good>
>>
>> > <good>
>> > <name>Ice</name>
>> > <value>24</value>
>> > <tech>1</tech>
>> > </good>
>>
>> > <good>
>> > <name>Water</name>
>> > <value>30</value>
>> > <tech>1</tech>
>> > </good>
>>
>> > </economy>
>>
>> > code:
>>
>> > // The list is loaded and valid. checked the names and the xml output
>> > already
>> > //
>> > void DataManager::ParseGoods(MSXML2::IXMLDOMNodeListPtr &
>> > goodXMLNodes)
>> > {
>> > for(Good::ID id = 0; id < static_cast<unsigned>(goodXMLNodes-
>> >>Getlength()); ++id)
>> > {
>> > MSXML2::IXMLDOMNodePtr node = goodXMLNodes->nextNode();
>>
>> > Good good;
>> > good.m_ID = id;
>>
>> > std::string nodeName = node->nodeName;
>> > std::string nodeText = node->text;
>>
>> > for(node = node->GetfirstChild(); node != NULL; node = node-
>> >>GetnextSibling())
>> > {
>> > nodeName = node->nodeName;
>>
>> > if( nodeName == "name" || nodeName == "Name" )
>> > {
>> > good.m_name = node->text;
>> > }
>>
>> > if( nodeName == "value" || nodeName == "Value" )
>> > {
>> > // This throws an exception. Nowhere on MSDN can I find
>> > what kind of exception or why it was thrown.
>> > // I assume there is some conversion problem...
>> > // How do I get my unsigned int out here?
>> > good.m_baseValue = node->GetnodeValue().operator unsigned
>> > int();
>>
>> > }
>> > }
>>
>> > // Check that all fields got filled out
>>
>> > }
>> > }
>>
>> Elements don't have a value, they can have child text nodes which have a
>> value though.
>> I don't really understand C++ but using DomDocument you can either get
>> the
>> element's firstChild, if that happens to be a text node and get its value
>> of
>> use the nodeTypedValue on the element.
>> Unless your elements have been validated against a schema the
>> nodeTypedValue
>> for an element will be a string so you'll have to convert that to an int
>> yourself.
>>
>> --
>>
>> Joe Fawcett (MVP - XML)http://joe.fawcett.name- Hide quoted text -
>>
>> - Show quoted text -
>
>
> Ok, I added a schema, validated it, went through the mess of trying to
> figure out comm exceptions and types.
>
> Now, you say that elements don't have a value., and I believe you
> don't get me wrong...
> Why does it successfully get the value for the <value> element and
> <tech> elements now, but not the <name> element? To prove that, you
> have to comment out the <name> element parsing, which is failing now.
> I did successfully get the correct values for the other two elements.
>
> There still stands an error with parsing the <name> element though,
> which I don't understand. the COM exception reports a 'type mismatch'.
> So, I tried taking your advise and getting the child of the <name>
> node and then getTypedValue() from that and I still get the 'type
> mismatch' com exception. I attempted to get the data type and it
> reported VT_NULL. This is killing me. 3 days trying to parse 3 values
> from an xml file!
>
>
> ---------------------------------------------------
> xml schema:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> targetNamespace="urn:goods"
> xmlns:goods="urn:goods">
>
> <xsd:element name="goods" type="goods:GoodsType"/>
>
> <xsd:complexType name="GoodsType">
> <xsd:sequence>
> <xsd:element name="good"
> type="goods:GoodType"
> minOccurs="1"
> maxOccurs="unbounded"/>
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:complexType name="GoodType">
> <xsd:sequence>
> <xsd:element name="name" type="xsd:string"/>
> <xsd:element name="value" type="xsd:float"/>
> <xsd:element name="tech" type="xsd:unsignedInt"/>
> </xsd:sequence>
> </xsd:complexType>
>
> </xsd:schema>
>
> -------------------------------------------------------------------------------------------
> xml file:
>
> <?xml version="1.0" encoding="utf-8"?>
>
> <x:goods xmlns:x="urn:goods"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="urn:goods file:./goods.xsd">
>
> <!-- Goods
> name - Name of the good
> value - Base monetary value of the good. This value is modified by
> other factors to calculate a buy and sell price
> tech - Required tech level for a planet to produce this good
> -->
> <good>
> <name>Energy</name>
> <value>12</value>
> <tech>1</tech>
> </good>
>
> <good>
> <name>Ice</name>
> <value>24</value>
> <tech>1</tech>
> </good>
>
> <good>
> <name>Water</name>
> <value>30</value>
> <tech>1</tech>
> </good>
>
> </x:goods>
>
> ------------------------------------------------------------
>
> parsing code:
>
> //--------------------------------------------------------------------------------------------------------------------
> void DataManager::ParseGoods(MSXML2::IXMLDOMNodeListPtr &
> goodXMLNodes)
> {
> BaseException error("Unspecified Error",
> "void
> DataManager::ParseGoods(MSXML2::IXMLDOMNodeListPtr & goodXMLNodes)",
> "DataManager.cpp");
>
> for(Good::ID id = 0; id < static_cast<unsigned>(goodXMLNodes-
>>Getlength()); ++id)
> {
> MSXML2::IXMLDOMNodePtr node = goodXMLNodes->nextNode();
>
> Good good;
> good.m_ID = id;
>
> for(node = node->GetfirstChild(); node != NULL; node = node-
>>GetnextSibling())
> {
> std::string element = node->nodeName;
>
> if( element == "name" || element == "Name" )
> {
> try
> {
> // *********************************FAILS
> HERE*************************************
> good.m_name = node->GetnodeTypedValue();
>
> // Also failes if I do this:
> // good.m_name = node->GetfirstChild()-
>>GetnodeTypedValue();
> //
> // good::m_name is a std::string
>
> }
> catch(_com_error & e)
> {
> std::ostringstream errorMsg;
> errorMsg << "Error parsing <good> node #" << id << ".
> \n"
> << "Could not get typed value of <name>
> element.\n\n"
> << "COM error description: " <<
> e.ErrorMessage();
>
> error.m_msg = errorMsg.str();
> throw error;
> }
> }
>
> if( element == "value" || element == "Value" )
> {
> try
> {
> // **************** These seem to work fine!!!
> *******************************************
> good.m_baseValue = node->GetnodeTypedValue();
> }
> catch(_com_error & e)
> {
> std::ostringstream errorMsg;
> errorMsg << "Error parsing <good> node #" << id << ".
> \n"
> << "Could not get typed value of <value>
> element.\n\n"
> << "COM error description: " <<
> e.ErrorMessage();
>
> error.m_msg = errorMsg.str();
> throw error;
> }
> }
>
> if( element == "tech" || element == "Tech" )
> {
> try
> {
> good.m_requiredTechLevel = node->GetnodeTypedValue();
> }
> catch(_com_error & e)
> {
> std::ostringstream errorMsg;
> errorMsg << "Error parsing <good> node #" << id << ".
> \n"
> << "Could not get typed value of <tech>
> element.\n\n"
> << "COM error description: " <<
> e.Description();
>
> error.m_msg = errorMsg.str();
> throw error;
> }
> }
> }
>
> // Check that all fields got filled out
>
>
> }
> }
>
>
>
>
Can we try a new approach?
What exactly are you trying to achieve? Do you just want to loop through all
'good' elements and show the values of name, value and tech?
If so here is the HTML/JavaScript code, you'll need to translate to C++
though.
<html>
<head>
<title>Parse Economy</title>
<script type="text/javascript">
function parseXml(xml)
{
var xmlDoc = new ActiveXObject("msxml2.domdocument.3.0");
xmlDoc.setProperty("SelectionLanguage", "XPath");
var bLoaded = xmlDoc.loadXML(xml);
if (!bLoaded)
{
showParseError(xmlDoc.parseError);
return
}
var xpath = "/*/good";
var colGood = xmlDoc.selectNodes(xpath);
var firstWay = "";
var secondWay = "";
var l = colGood.length;
alert("Found " + l + " good elements");
for (var i = 0; i < l; i++)
{
var oGood = colGood[i];
var oName = oGood.selectSingleNode("name");
var oValue = oGood.selectSingleNode("value");
var oTech = oGood.selectSingleNode("tech");
firstWay += "good #" + (i + 1) + ": name = " + oName.nodeTypedValue
+ ", value = " + oValue.nodeTypedValue + ", tech = " + oTech.nodeTypedValue
+ "\n";
secondWay += "good #" + (i + 1) + ": name = " +
oName.firstChild.nodeValue + ", value = " + oValue.firstChild.nodeValue + ",
tech = " + oTech.firstChild.nodeValue + "\n";
}
alert("Using nodeTypedValue:\n" + firstWay);
alert("Using firstChild & nodeValue:\n" + secondWay);
}
function showParseError(error)
{
var sMessage = "Error loading document:\n\tReason: " + error.reason;
sMessage += "\n\tSource: " + error.srcText;
sMessage += "\n\tLine: " + error.line;
alert(sMessage);
}
</script>
</head>
<body>
<textarea rows="20" cols="50" id="txtXml">
<?xml version="1.0" encoding="utf-8"?>
<economy>
<good>
<name>Energy</name>
<value>12</value>
<tech>1</tech>
</good>
<good>
<name>Ice</name>
<value>24</value>
<tech>1</tech>
</good>
<good>
<name>Water</name>
<value>30</value>
<tech>1</tech>
</good>
</economy>
</textarea>
<input type="button" value="Parse XML"
onclick="parseXml(document.getElementById('txtXml').value);" />
</body>
</html>
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
