Extract Info from XML File Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Extract Info from XML File

26 Apr 2023, 09:31

I am trying to extract only the Client Name from the following XML file and have it pop-up in a message box (or store in a script for later use):

Code: Select all

<?xml version="1.0"?>
<ToolStatusReport xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <GlobalInformation>
    <UserName>ADMIN</UserName>
    <ComputerName>LAPTOP01</ComputerName>
    <ProjectNumber>0-1000-00001</ProjectNumber>
    <ClientName>Burger King</ClientName>

In my case, the message box shows the entire line with identifiers rather than just the info by itself:

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script at one time

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
MsgBox, % ClientName
return
Current result: <ClientName>Burger King</ClientName>
Preferred result: Burger King

How can I isolate the information by itself without the identifiers?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Extract Info from XML File

26 Apr 2023, 10:02

Code: Select all

XMLData := "
(
<?xml version=""1.0""?>
<ToolStatusReport xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<GlobalInformation>
		<UserName>ADMIN</UserName>
		<ComputerName>LAPTOP01</ComputerName>
		<ProjectNumber>0-1000-00001</ProjectNumber>
		<ClientName>Burger King</ClientName>
	</GlobalInformation>	
</ToolStatusReport>
)"

xmlDoc := ComObjCreate("MSXML2.DOMDocument.6.0")
xmlDoc.loadXML(XMLData)
myErr := xmlDoc.parseError
if myErr.reason
	MsgBox % "You have error "  myErr.reason

MsgBox % xmlDoc.getElementsByTagName("ClientName").item[0].text

; or for multiple incidents of <UserName>
for e in xmlDoc.getElementsByTagName("ClientName")
	MsgBox % e.text
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

26 Apr 2023, 10:08

The XML file listed in the OP is just a partial...

I am looking for a solution to my AHK script that searches the XML information and populates the Client Name in a message box without the tags included.
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

26 Apr 2023, 10:24

Apparently the solution is as simple as adding a "1" next to the name in the msg box line :headwall: :

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script at one time

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
MsgBox, % ClientName1
return
Now the msg box shows "Burger King" instead of "<ClientName>Burger King</ClientName>"

:superhappy:
RussF
Posts: 1294
Joined: 05 Aug 2021, 06:36

Re: Extract Info from XML File

26 Apr 2023, 10:27

It worked for me...
image.png
image.png (2.41 KiB) Viewed 766 times
Russ
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Extract Info from XML File

26 Apr 2023, 10:28

it should still work, anyhow just add "1" after ClientName in your code to capture the subpattern

Code: Select all

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
MsgBox, % ClientName1
return
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

26 Apr 2023, 10:40

Thank you all!
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

11 May 2023, 15:48

BeRoM wrote:
26 Apr 2023, 10:24
Apparently the solution is as simple as adding a "1" next to the name in the msg box line :headwall: :

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script at one time

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
MsgBox, % ClientName1
return
Now the msg box shows "Burger King" instead of "<ClientName>Burger King</ClientName>"

:superhappy:
Weird issue...

Do any of you have an idea of why this script would work perfectly fine on my local PC, but it does not work on a VM or another PC via Remote Desktop Connection (RDC)?
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Extract Info from XML File

11 May 2023, 20:53

BeRoM wrote: Weird issue...

Do any of you have an idea of why this script would work perfectly fine on my local PC, but it does not work on a VM or another PC via Remote Desktop Connection (RDC)?
Saying "does not work" is not giving us much to go on. How specifically did it not work? Did it produce errors? If so, what specifically? If not, what did it do? Display an empty MsgBox? Did you try checking the values of the other variables (XMLData, Clipboard) before the final MsgBox?
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

12 May 2023, 09:47

boiler wrote:
11 May 2023, 20:53
BeRoM wrote: Weird issue...

Do any of you have an idea of why this script would work perfectly fine on my local PC, but it does not work on a VM or another PC via Remote Desktop Connection (RDC)?
Saying "does not work" is not giving us much to go on. How specifically did it not work? Did it produce errors? If so, what specifically? If not, what did it do? Display an empty MsgBox? Did you try checking the values of the other variables (XMLData, Clipboard) before the final MsgBox?
No errors, but yes, resulting in an empty message box.

Interestingly, when I include another line to the script to capture another subpattern, it works only on the 2nd subpattern, but not the first.

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script at one time

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
RegExMatch(XMLData, "s)<ProjectNumber>(.*?)</ProjectNumber>", ProjectNumber) ; Parse for ProjectNumber
MsgBox, % ClientName1
MsgBox, % ProjectNumber1
return
In this case, if I ran this script on my VM/RDC, the first message box for ClientName is blank, but the second is correct for ProjectNumber.

All works perfectly when ran on my local PC - but I really want the VM working since I use it much more :problem: .
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Extract Info from XML File

12 May 2023, 10:34

Did you not see this, or just choose not to answer it?
boiler wrote: Did you try checking the values of the other variables (XMLData, Clipboard) before the final MsgBox?
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

12 May 2023, 10:49

boiler wrote:
12 May 2023, 10:34
Did you not see this, or just choose not to answer it?
boiler wrote: Did you try checking the values of the other variables (XMLData, Clipboard) before the final MsgBox?
Didn't see it - but all other variables are good.

The only difference is which PC is running the script... Local PC or VM - 100% success on the Local PC, but blank info for ClientName on VM.
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File

12 May 2023, 11:10

Ok, I think we're getting somewhere.

I decided to try removing the 1 from " MsgBox, % ClientName1 " to change it to " MsgBox, % ClientName " to see if it indeed is capturing the correct info, and interestingly the result now shows my Local PC Name (LAPTOP01)! So, I think there is a conflict, when I am connected to a VM, the script is somehow capturing my Local PC name as "ClientName" instead of the ClientName tag inside the XML file.

Any ideas on how to force AHK to capture the ClientName from the XML and not my PC Name?
gregster
Posts: 9086
Joined: 30 Sep 2013, 06:48

Re: Extract Info from XML File

12 May 2023, 11:32

I would try #NoEnv. But the fact that it uses the environment variable probably means that the variable is otherwise empty.
Avoids checking empty variables to see if they are environment variables (recommended for all new scripts).
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Extract Info from XML File  Topic is solved

12 May 2023, 13:17

gregster wrote:
12 May 2023, 11:32
I would try #NoEnv. But the fact that it uses the environment variable probably means that the variable is otherwise empty.
Avoids checking empty variables to see if they are environment variables (recommended for all new scripts).
Nailed it. Adding #NoEnv just above the MsgBox line made it work perfectly on Local and VM. Thank you, gregster.

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script at one time

FileRead, XMLData, %clipboard%/Status.xml ; Read XML file
RegExMatch(XMLData, "s)<ClientName>(.*?)</ClientName>", ClientName) ; Parse for Client
RegExMatch(XMLData, "s)<ProjectNumber>(.*?)</ProjectNumber>", ProjectNumber) ; Parse for ProjectNumber
#NoEnv
MsgBox, % ClientName1
MsgBox, % ProjectNumber1
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Descolada, just me, Rohwedder and 110 guests