Namespace xml extract -- help

M

Mag Gam

I have a file similar to this:

<Customer xmlns="http://www.localsite.com/ecommerce/2006-v01/
organization">
<ns1:Contract xmlns:ns1="http://www.localsite.com/ecommerce/2006-v01/
portfolio">
<ns1:ContractId>
<ns2:Identifier xmlns:ns2="http://www.localsite.com/ecommerce/2006-
v01/common">199</ns2:Identifier>
</ns1:ContractId>
<ns1:OrganizationId>
<ns3:Identifier xmlns:ns3="http://www.localsite.com/ecommerce/2006-
v01/common">6142</ns3:Identifier>
</ns1:OrganizationId>
</ns1:Contract>
</Customer>

How can I extract, Identifier field's data?

It seems I am not able to extract using DOM, because of Namespaces.
Any help would be appreciated.

TIA
 
J

Joe Kesselman

Mag said:
It seems I am not able to extract using DOM, because of Namespaces.

It seems you aren't doing your homework.

DOM Level 2 and later are fully namespace-aware, if built using the
namespace-aware APIs and/or a namespace-aware parser. Look at any decent
DOM tutorial. If you don't understand namespaces, look at a good XML
tutorial as well.
 
M

Mag Gam

J

Joe Kesselman

Mag said:
Thanks! As long as I am on the right track I should do well. For
Namespaces, I will use http://www.w3schools.com/xml/xml_namespaces.asp
and http://www.w3schools.com/dom/dom_examples.asp

I did say find a *good* tutorial. Unfortunately that isn't one.

The DOM examples they show are obsolete; they use the DOM Level 1
non-namespaced create-element method, for example, which isn't
compatable with namespace-aware processing. These days you shouldn't
touch createElement(); you should only be using createElementNS(). Ditto
for other methods replaced by ...NS() versions.


Yes, I admit, this is partly the DOM WG's fault for not clearly
deprecating the old non-NS calls. We wanted to do so, but we got a huge
amount of pushback from folks who pointed out that the old calls were
still legitimate for (and ONLY for) backward-compatability with
pre-existing DOM Level 1 code. The best agreement we could get from them
was agreement that the DOM Level 2 spec should include a warning ... but
not enough people actually read the spec. Growl.
 
M

Martin Honnen

Mag said:
I have a file similar to this:

<Customer xmlns="http://www.localsite.com/ecommerce/2006-v01/
organization">
<ns1:Contract xmlns:ns1="http://www.localsite.com/ecommerce/2006-v01/
portfolio">
<ns1:ContractId>
<ns2:Identifier xmlns:ns2="http://www.localsite.com/ecommerce/2006-
v01/common">199</ns2:Identifier>
</ns1:ContractId>
<ns1:OrganizationId>
<ns3:Identifier xmlns:ns3="http://www.localsite.com/ecommerce/2006-
v01/common">6142</ns3:Identifier>
</ns1:OrganizationId>
</ns1:Contract>
</Customer>

How can I extract, Identifier field's data?

Here is JavaScript that runs in W3C DOM Level 2 compliant browsers like
Mozilla or like Opera:

var identifiers =
xmlDocument.getElementsByTagNameNS('http://www.localsite.com/ecommerce/2006-v01/common',
'Identifier');
for (var i = 0, l = identifiers.length; i < l; i++) {
alert(identifiers.firstChild.nodeValue);
}

You could for instance load the XML document with XMLHttpRequest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top