Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot the node list for an element

Status
Not open for further replies.

xtreemnet

Programmer
Aug 9, 2003
88
NZ
Hi

I am new to XML. I have a file with first few lines like:

<?xml version="1.0" ?>
<LandXML xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="1.0" date="2002-07-12" time="12:48:14" readOnly="true" language="English" LandXMLId="2000003">
<Units>
<Metric areaUnit="squareMeter" linearUnit="meter" volumeUnit="liter" temperatureUnit="celsius" pressureUnit="milliBars" angularUnit="decimal dd.mm.ss" directionUnit="decimal dd.mm.ss"/>
</Units>

I am trying to use DOM to access the information. The following code does not find "Units" element.

Public objNodes As IXMLDOMNodeList
Public objBookNode As IXMLDOMNode
Public objADORS As New ADODB.Recordset
Public objADOCnn As New ADODB.Connection


objXMLDOM.async = False

If Not objXMLDOM.Load("D:\Bernie\More XML Files\Extract 4 Surveys.xml") Then
MsgBox "Error: " & objXMLDOM.parseError.reason
Exit Sub
End If

Set objNodes = objXMLDOM.selectNodes("/LandXML/Units")

If objNodes.Length <= 0 Then
MsgBox "No records to be inserted!"
Exit Sub
End If

When I execute this code I get the length of the node list as zero. But if I remove the stuff after "LandXML" in the second line and just have <LandXML>, it works OK. Is there any way to avoid this?

Thanks,
 
The xml source seems to be incorrect:
Code:
xmlns="[URL unfurl="true"]http://www.landxml.org/schema/LandXML-1.0"[/URL]
should be something like:
Code:
xmlns:somename="[URL unfurl="true"]http://www.landxml.org/schema/LandXML-1.0"[/URL]
With this little adaption, I had Dom find the 'Units' node without any problem.
By the way, as the problem seems to be the LandXML-node, this works without modifying the xml:
Code:
Set objNodes = objXMLDOM.selectNodes("//Units")
Try and find out why these namespaces are declared, and what they refer to (in your example you can leave them out, because they're not used at all).

Good luck
 
Thanks for the reply. It works with modification, but I would like to avoid that. The "//Units" did not work for me. I am using MSXML v4.0. Can you see why?
 
My mistake, I must have forgotten to restore the original xml.

It seems that DOM doesn't want to recognize nodes like this:
Set objNodes = objXMLDOM.selectNodes("/LandXML/Units")
but it does work with:
Set objNodes = objXMLDOM.selectNodes("/*/*")
and even with:
Set objNodes = objXMLDOM.selectNodes("/*[name()='LandXML']/*[name()='Units']")

I really don't know what the differnce would be between the first and the last expression, but MS knows best...

Anyway, modification seems to be the right way to go: why work with source-files that you know are corrupt?
 
That's great. It works now with your code. But I guess you are right in saying that not to deal with corrupt files.

Anyway thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top