Namespace causes XML DOM to fail

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2203
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Namespace causes XML DOM to fail

03 Nov 2015, 16:30

This is a follow-up to a previous thread. The code posted there by xml and MJs using ComObjCreate("MSXML2.DOMDocument.6.0") works perfectly on this sample XML data:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<OrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OrderedBy>
    <UserID>12345</UserID>
  </OrderedBy>
</OrderInfo>
But when I tried it on the "real" XML file, it failed, and I determined that the failure is because of a namespace in the <OrderedBy> tag. Here's the relevant subset of the real XML file (although I changed the URL of the namespace for privacy reasons - it is a client's website):

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<orderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OrderedBy xmlns="http://www.clientwebsite.com/">
    <UserID>12345</UserID>
  </OrderedBy>
</OrderInfo>
Here's a working script to use with the above XML (with thanks to xml and MJs for the code):

o:=ComObjCreate("MSXML2.DOMDocument.6.0")
o.async:=false
o.loadXML(data)
node:=o.selectSingleNode("//orderInfo/OrderedBy/UserID")
UserID:=Node.text
MsgBox,0,UserID,%UserID%

Why does the existence of the namespace (xmlns=) in <OrderedBy> cause the code to fail? How can I fix it? Thanks, Joe
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Namespace causes XML DOM to fail

03 Nov 2015, 16:56

http://autohotkey.com/board/topic/99806 ... /?p=625747
Jackie Sztuk _Blackholyman wrote: So with your xml docment you need to set the Namespace property to use the xpath method "selectSingleNode"

the xml namespace is defined in the root element here "<adc_database xmlns="http://www.accuweather.com">"

example

Code: Select all

xmldata := UrlDownloadToVar("http://thale.accu-weather.com/widget/thale/weather-data.asp?location=37201")

doc := loadXML(xmldata)

namespace := "xmlns:ns='" doc.documentElement.namespaceURI "'"
doc.setProperty("SelectionNamespaces", namespace)

MsgBox % "speed " doc.selectSingleNode("//ns:speed").text
MsgBox % "dist " doc.selectSingleNode("//ns:dist").text
return


loadXML(ByRef data)
{
  o := ComObjCreate("MSXML2.DOMDocument.6.0")
  o.async := false
  o.loadXML(data)
  return o
}

UrlDownloadToVar(URL){
    ComObjError(False)
    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("GET",URL, False)
    WebRequest.Send()
    Return WebRequest.ResponseText, ComObjError(1)
    }
you can also remove the namespace before loading the xml

like this

Code: Select all

xmldata := UrlDownloadToVar("http://thale.accu-weather.com/widget/thale/weather-data.asp?location=37201")

ns = xmlns="http://www.accuweather.com"
xmldata := RegExReplace(xmldata, ns, "")

doc := loadXML(xmldata)

MsgBox % "speed " doc.selectSingleNode("//speed").text
MsgBox % "dist " doc.selectSingleNode("//dist").text
return


loadXML(ByRef data)
{
  o := ComObjCreate("MSXML2.DOMDocument.6.0")
  o.async := false
  o.loadXML(data)
  return o
}

UrlDownloadToVar(URL){
    ComObjError(False)
    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("GET",URL, False)
    WebRequest.Send()
    Return WebRequest.ResponseText, ComObjError(1)
    }
hope it helps
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
JoeWinograd
Posts: 2203
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Namespace causes XML DOM to fail

03 Nov 2015, 17:37

Yes, definitely helps! I like the idea of removing the namespace. I hope you can help with a more general RegExReplace call that would delete everything starting at the first space after a "<" and before a ">" — Regular Expressions are not my strength. :) Thanks, Joe
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Namespace causes XML DOM to fail

03 Nov 2015, 18:17

You can try it like this

Code: Select all

ns = xmlns=".*?"
xmldata := RegExReplace(xmldata, ns, "")
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
JoeWinograd
Posts: 2203
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Namespace causes XML DOM to fail

03 Nov 2015, 18:40

Excellent! Thanks very much. I changed it slightly to also remove the space:

Code: Select all

ns:=" xmlns="".*?"""

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], norot41087, Rohwedder and 124 guests