XML parsing error when node name contains colon

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Drugwash
Posts: 675
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

XML parsing error when node name contains colon

03 Dec 2017, 04:05

Hey all, it's the first time I'm working with XML and objects and all. I've got most things figured out but one issue still stands out as pointed out in the title.

The XML file I'm trying to parse uses colons in certain node names. I need to get the text out of some of those nodes and the direct method always fails with error 0x80004005 (Unspecified error) in msxml6.dll stating Reference to undeclared namespace prefix: 'xxx'.

Node names are in the form of xxx.nodename and error pops out when I'm trying
outVar := doc.selectSingleNode(parentNode "/xxx:nodename").text

However, if I use a loop like this, it works without error:

Code: Select all

doc := ComObjCreate("MSXML2.DOMDocument.6.0"), doc.async := false, doc.Load(testfile)
n := doc.selectSingleNode(parentNode)
Loop
	{
	if !a := n.childNodes.item(A_Index-1)
		break
	if (a.nodeName = "someNode")
		var1 := a.text
	if (a.nodeName = "otherNode")
		var2 := a.text
	if (a.nodeName = "xxx:nodename")
		outVar := a.text
	}
Obviously I'd like to avoid looping through all child nodes since they can be in a huge number (input XML is about 23MB) and I only need a handful of them.
I wonder where the bug or limitation is: in my script, in AHK or in Windows' XML parser? I'm working with XP-SP3 for this script.

Thanks for any help you can offer.

P.S. Just remembered one other snag: there's no way to make use of such node names in a statement like if a.nodeName in xxx:nodename,someNode,otherNode because AHK throws another error. :(

P.P.S. A friend of mine tried to use the script (which works fairly well on my XP) under Ubuntu/WINE and it failed with a 10MB XML file but worked correctly with a 1MB file. Is there an ErrorLevel or any kind of error trapping when an object cannot be created or a file cannot be loaded as an object?
Last edited by Drugwash on 03 Dec 2017, 13:01, edited 1 time in total.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
User avatar
Capn Odin
Posts: 1291
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: XML parsing error when node name contains colon

03 Dec 2017, 06:19

Sorry but I don't have time to try and fix it but here are some links.

The method https://msdn.microsoft.com/en-us/librar ... -snippet-1
XPath https://msdn.microsoft.com/en-us/librar ... .110).aspx

3/4 down the table is your situation.
Please excuse my spelling I am dyslexic.
User avatar
Drugwash
Posts: 675
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: XML parsing error when node name contains colon

03 Dec 2017, 06:51

Thank you but it all looks way too complicated to me and can't make heads or tails of all that gibberish. :(
For what it's worth it's about parsing a Wordpress blog export file which contains nodes prefixed with wp, dc, excerpt, content.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
User avatar
nnnik
Posts: 3871
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: XML parsing error when node name contains colon

06 Dec 2017, 05:08

You could just remove the : from all tag names prior to loading.
Recommends AHK Studio
User avatar
Drugwash
Posts: 675
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: XML parsing error when node name contains colon

06 Dec 2017, 06:44

That'd be a workaround, not a solution and given how many such tags could be and the possibility of interference with the actual text, it would pose the risk of altering valid contents.
However, in the mean time I found an alternative and hopefully faster way of dealing with nodes and text that doesn't stumble into this issue.
The other issue remains, generically, namely the inability to use a xxx:yyy string in a if var in command, although I'm not using that either.

Dunno about the WINE issue, I think ComObjError() is enabled by default? In that case there would've been a popup if something went wrong, but it wasn't. However I noticed even in XP the RegExMatch() function takes an abnormal amount of RAM and page file when searching a (large) string for matches so that may be one/the reason for the WINE failure. It needs further testing though.

Thank you for your input, please chime in at any time if you get any other ideas.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
dominicx
Posts: 7
Joined: 16 Aug 2017, 07:53

Re: XML parsing error when node name contains colon

19 Dec 2017, 05:06

Although my query is not related to the actual question but perhaps you could help. In my situation I have xml information online which I can access through web browser. There is a dynamic link eg. "www.example.com/service/refNo" When I access this file i get data set which looks like this
<?xml version="1.0"?>
-<NewDataSet>
-<Table>
<refNo>number</refNo>
<data1>data</data1>
</Table>
</NewDataSet>

How can I put information from data1 to variable?

I am using code which looks like this right now, however I get blank message on variableA. I guess I am not loading web information properly, however I can not find any information how to pull this information from web link. Any help would be appreciated.

Code: Select all

^k::
Clipboard := ""
Send, ^c
Clipwait
refno = %Clipboard%
sleep, 10
xml := "www.example.com/service"
link := xml . refno
xmlA := ComObjCreate("MSXML2.DOMDocument.6.0")
xmlA.async := false
xmlA.loadXML("link")
variableA := xmlA.selectSingleNode("NewDataSet/Table/data1")
msgbox %VariableA%
return
User avatar
Drugwash
Posts: 675
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: XML parsing error when node name contains colon

20 Dec 2017, 05:06

Simplest way would be to use variableA := xmlA.selectSingleNode("NewDataSet/Table/data1").text
That, however, will not allow selecting prefixed nodes such as <prefix:data1>, which is precisely the problem that got me starting this topic.
I solved it by using var := xmlA.item(0).getElementsByTagName("data1").item(0).childNodes.item(0).nodeValue which allows the use of prefixed node names.
To use that in a loop, try:

Code: Select all

n := xmlA.getElementsByTagName("Table")
Loop, % n.length
  var%A_Index% := n.item(A_Index-1).getElementsByTagName("data1").item(0).childNodes.item(0).nodeValue
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], masterross, swagfag and 130 guests