Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data Extraction Question

1 view
Skip to first unread message

TC

unread,
Dec 23, 2007, 6:09:45 PM12/23/07
to
Hey All,

I have a basic XML question.

Let's say that I have an XML document where the root element text / node
text is "<MyNamespace:MyXml>MyData</MyNamespace:MyXml>".

Therefore, let's say that the following:

Set xmlRootElement = xmlDoc.documentElement
Debug.Print xmlRootElement.Text

and, therefore, xmlRootElement.Text =
"<MyNamespace:MyXml>MyData</MyNamespace:MyXml>"
and, by same token, the following:

For Each xmlNode In xmlDoc.childNodes
Debug.Print xmlNode.Text
Next xmlNode

and, therefore, xmlNode.Text =
"<MyNamespace:MyXml>MyData</MyNamespace:MyXml>"

Considering the above, I want to know how to extract just the value embedded
in that text (i.e. "MyData"). Do I have to use traditional string parsing
or is there a way using the XML libraries to quickly say SomeObject.Value or
SomeObject.Text and Voila! I get the "MyData" value?

Thanks,

TC


Joe Fawcett

unread,
Dec 24, 2007, 4:57:55 AM12/24/07
to
"TC" <getmye...@yahoo.com> wrote in message
news:#lompjbR...@TK2MSFTNGP03.phx.gbl...
Firstly the XML is not well-formed as the namespace indicated by MyNamepace
is not declared, it should be something like:
<MyNamespace:MyXml
xmlns:MyNamespace="http://myDomain.com/namespaceString">MyData</MyNamespace:MyXml>
It depends somewhat on what parser you are using but if using MSXML the text
value of the documentElement will be 'MyData'.
For more precise acquisition of nodes you should use XPath in conjuction
with SelectNodes/SelectSingleNode.
This test script should demonstrate the above:


var _oDom = null
function getSyncDom()
{
if (!_oDom)
{
_oDom = new ActiveXObject("Msxml2.DomDocument.6.0");
_oDom.async = false;
}
return _oDom.cloneNode(false);
}

function showParseError(error)
{
var sMessage = "Error loading document:\n\tReason: " + error.reason;
sMessage += "\n\tSource: " + error.srcText;
sMessage += "\n\tLine: " + error.line;
WScript.echo(sMessage);
}

function main()
{
var oDoc = getSyncDom();
var bLoaded = oDoc.loadXML('<MyNamespace:MyXml
xmlns:MyNamespace="http://myDomain.com/namespaceString">MyData</MyNamespace:MyXml>');
if (bLoaded)
{
WScript.echo(oDoc.documentElement.text);
}
else
{
showParseError(oDoc.parseError);
}
}

main();

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Anthony Jones

unread,
Dec 24, 2007, 6:31:05 AM12/24/07
to
"TC" <getmye...@yahoo.com> wrote in message
news:%23lompjb...@TK2MSFTNGP03.phx.gbl...

I think you've miss understood the .Text property. It simply concatenates
the values of all the text nodes inside the element and returns the result
(or the value of the node if its an Attribute).

Hence xmlRootElement.Text is "MyData".


--
Anthony Jones - MVP ASP/ASP.NET


0 new messages