Discussion:
Parsing XML DOM with Scoped Namespaces using VB6 and MSXML
(too old to reply)
Mike
2004-10-13 15:18:48 UTC
Permalink
Phew. Subject title kinda says it all really...

I am trying to parse an XML file, which contains multiple namespaces. Of
these, there is always a default namespace which contains (sometimes)
multiple scoped entities.

I know I cannot use GetElementsByTagName as this always returns nothing as
it cannot work with default namespaces. Does anyone know a workaround for
this?

My next workaround would be to develop a "find" function to recurse through
the DOM tree and return (as a collection of nodes) each node that matches
the name. Does anyone have a function to do this?

If I attempt to call objDOMDocument.setProperty - I cannot set an alias to
the default namespace as it contains multiple ":" characters! Therefore, I
cannot use .selectsinglenode as I cannot specify the alias to the default
namespace, because the default namespace contains colon characters, and this
is not allowed to be set using the setProperty property of the
DOMDocument50.

Any ideas are warmly welcomed.

Thakns in advance for your time!

Mike :)
Martin Honnen
2004-10-14 11:58:31 UTC
Permalink
Mike wrote:

=
Post by Mike
I am trying to parse an XML file, which contains multiple namespaces. Of
these, there is always a default namespace which contains (sometimes)
multiple scoped entities.
I know I cannot use GetElementsByTagName as this always returns nothing as
it cannot work with default namespaces. Does anyone know a workaround for
this?
Yes, use selectSingleNode/selectNodes which takes an XPath expression as
the argument after making sure you have declared the namespaces for
selection e.g. JScript

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;
xmlDocument.loadXML('<gods xmlns="http://example.com/2004/10/gods"><god
name="Kibo" /><dv:devil xmlns:dv="http://example.com/2004/10/devils"
name="Xibo" /></gods>');

xmlDocument.setProperty('SelectionLanguage', 'XPath');
xmlDocument.setProperty('SelectionNamespaces',
'xmlns:gd="http://example.com/2004/10/gods"
xmlns:dv="http://example.com/2004/10/devils"');

var el1 = xmlDocument.selectSingleNode('//gd:god[@name = "Kibo"]');
alert(el1.nodeName);

var devils = xmlDocument.selectNodes('//dv:devil');
for (var i = 0; i < devils.length; i++) {
alert(devils[i].nodeName);
}
--
Martin Honnen
http://JavaScript.FAQTs.com/
Mike [MCP VB]
2004-10-21 14:01:20 UTC
Permalink
Post by Martin Honnen
Yes, use selectSingleNode/selectNodes which takes an XPath expression as
the argument after making sure you have declared the namespaces for
selection e.g. JScript
var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;
xmlDocument.loadXML('<gods xmlns="http://example.com/2004/10/gods"><god
name="Kibo" /><dv:devil xmlns:dv="http://example.com/2004/10/devils"
name="Xibo" /></gods>');
xmlDocument.setProperty('SelectionLanguage', 'XPath');
xmlDocument.setProperty('SelectionNamespaces',
'xmlns:gd="http://example.com/2004/10/gods"
xmlns:dv="http://example.com/2004/10/devils"');
Thanks. That's just the ticket - I was not using the correct syntax for
.setProperty.

Mike
d***@gmail.com
2017-11-07 11:31:35 UTC
Permalink
13 Years later and this post helped someone.
Thanks

Loading...