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? Re: How do I get the value out of an IXMLDOMNodePtr?To: NULL Date: 11/3/2008 1:42:00 PM On Nov 2, 5:29=A0am, "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>
> > =A0 <name>Energy</name>
> > =A0 <value>12</value>
> > =A0 <tech>1</tech>
> > </good>
>
> > <good>
> > =A0 <name>Ice</name>
> > =A0 <value>24</value>
> > =A0 <tech>1</tech>
> > </good>
>
> > <good>
> > =A0 <name>Water</name>
> > =A0 <value>30</value>
> > =A0 <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)
> > {
> > =A0 for(Good::ID id =3D 0; id < static_cast<unsigned>(goodXMLNodes-
> >>Getlength()); ++id)
> > =A0 {
> > =A0 =A0 =A0MSXML2::IXMLDOMNodePtr node =3D goodXMLNodes->nextNode();
>
> > =A0 =A0 =A0Good good;
> > =A0 =A0 =A0good.m_ID =3D id;
>
> > =A0 =A0 =A0std::string nodeName =3D node->nodeName;
> > =A0 =A0 =A0std::string nodeText =3D node->text;
>
> > =A0 =A0 =A0for(node =3D node->GetfirstChild(); node !=3D NULL; node =3D=
node-
> >>GetnextSibling())
> > =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0 nodeName =3D node->nodeName;
>
> > =A0 =A0 =A0 =A0 if( nodeName =3D=3D "name" || nodeName =3D=3D "Name" )
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0good.m_name =3D node->text;
> > =A0 =A0 =A0 =A0 }
>
> > =A0 =A0 =A0 =A0 if( nodeName =3D=3D "value" || nodeName =3D=3D "Value" =
)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0// This throws an exception. Nowhere on MSDN can=
I find
> > what kind of exception or why it was thrown.
> > =A0 =A0 =A0 =A0 =A0 =A0// I assume there is some conversion problem...
> > =A0 =A0 =A0 =A0 =A0 =A0// How do I get my unsigned int out here?
> > =A0 =A0 =A0 =A0 =A0 =A0good.m_baseValue =3D node->GetnodeValue().operat=
or unsigned
> > int();
>
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0}
>
> > =A0 =A0 =A0// Check that all fields got filled out
>
> > =A0 }
> > }
>
> 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 th=
e
> 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 nodeTypedVa=
lue
> 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=3D"1.0" encoding=3D"utf-8" ?>
<xsd:schema xmlns:xsd=3D"http://www.w3.org/2001/XMLSchema"
targetNamespace=3D"urn:goods"
xmlns:goods=3D"urn:goods">
<xsd:element name=3D"goods" type=3D"goods:GoodsType"/>
<xsd:complexType name=3D"GoodsType">
<xsd:sequence>
<xsd:element name=3D"good"
type=3D"goods:GoodType"
minOccurs=3D"1"
maxOccurs=3D"unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name=3D"GoodType">
<xsd:sequence>
<xsd:element name=3D"name" type=3D"xsd:string"/>
<xsd:element name=3D"value" type=3D"xsd:float"/>
<xsd:element name=3D"tech" type=3D"xsd:unsignedInt"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
---------------------------------------------------------------------------=
----------------
xml file:
<?xml version=3D"1.0" encoding=3D"utf-8"?>
<x:goods xmlns:x=3D"urn:goods"
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=3D"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 =3D 0; id < static_cast<unsigned>(goodXMLNodes-
>Getlength()); ++id)
{
MSXML2::IXMLDOMNodePtr node =3D goodXMLNodes->nextNode();
Good good;
good.m_ID =3D id;
for(node =3D node->GetfirstChild(); node !=3D NULL; node =3D node-
>GetnextSibling())
{
std::string element =3D node->nodeName;
if( element =3D=3D "name" || element =3D=3D "Name" )
{
try
{
// *********************************FAILS
HERE*************************************
good.m_name =3D node->GetnodeTypedValue();
// Also failes if I do this:
// good.m_name =3D 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 =3D errorMsg.str();
throw error;
}
}
if( element =3D=3D "value" || element =3D=3D "Value" )
{
try
{
// **************** These seem to work fine!!!
*******************************************
good.m_baseValue =3D 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 =3D errorMsg.str();
throw error;
}
}
if( element =3D=3D "tech" || element =3D=3D "Tech" )
{
try
{
good.m_requiredTechLevel =3D 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 =3D errorMsg.str();
throw error;
}
}
}
// Check that all fields got filled out
}
}
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
