How to locate a string within XML tags

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Trevor Hughes
Junior Coder
Posts: 25
Joined: Sun Dec 15, 2013 9:27 pm

How to locate a string within XML tags

Post by Trevor Hughes » Thu Aug 06, 2015 11:06 pm

Hello

I hope some-one can help with my question.

I am using SOAP to process some data entry. I want to use Macro Scheduler to run a SOAP project and then locate a string within an XML tag and copy the string to the clipboard. An example of the SOAP response is attached.

I want to try to find and copy the string within the tag <mac:MACValue.....> which in the example file attached is:
LugOc5uw+tMjwl7s502fS2V4lzi6Pv0mguCkGIOpvWU=

If anyone can offer some advice I would greatly appreciate it.

Kind regards
Trevor Hughes

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: How to locate a string within XML tags

Post by hagchr » Fri Aug 07, 2015 9:22 am

Hi, one way could be to use RegEx to extract it:

Code: Select all

ReadFile>C:\...\Example.xml,strFileContents
Let>tmp0=<mac:MACValue.+?">\K.+?(?=</)
RegEx>tmp0,strFileContents,0,m,nm,0
PutClipBoard>m_1

MDL>Just added to clipboard: %m_1%
(If you are new to RegEx then what it does is, look for <mac:MACValue followed by any character until "> then discard what you found (using \K) and look for any new character up until you find </, ie this will give your target string)

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: How to locate a string within XML tags

Post by hagchr » Fri Aug 07, 2015 9:58 am

... and another way could be to use XMLParse>

Code: Select all

LabelToVar>Test,sXML
StringReplace>sXML,soapenv:,,sXML
StringReplace>sXML,mac:,,sXML

Let>tmp0=Envelope/Body/MACValue/text()
XMLParse>sXML,tmp0,val,numBooks

PutClipBoard>val
MDL>Just added to clipboard: %val%

/*
Test:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
   </soapenv:Header>
   <soapenv:Body>
      <mac:MACValue xmlns:mac="http://somewebsite.govt.nz/jbms/msggate/MAC_Generation">LugOc5uw+tMjwl7s502fS2V4lzi6Pv0mguCkGIOpvWU=</mac:MACValue>
   </soapenv:Body>
</soapenv:Envelope>
*/

Trevor Hughes
Junior Coder
Posts: 25
Joined: Sun Dec 15, 2013 9:27 pm

Re: How to locate a string within XML tags

Post by Trevor Hughes » Sun Aug 09, 2015 9:00 pm

Thank you very much for the replies.

I am very keen to put these into a script as it will be a great time saver.

Kind regards
Trevor :D

Trevor Hughes
Junior Coder
Posts: 25
Joined: Sun Dec 15, 2013 9:27 pm

Re: How to locate a string within XML tags

Post by Trevor Hughes » Sun Aug 09, 2015 9:51 pm

Hello

Just a follow on question.

The script I'm hoping to build will have the SOAP project on screen and therefore what I would like to do is to locate the string within the XML Tags. Given the solutions kindly provided, I'm not sure how to apply these to an on-screen file rather than calling the XML file itself.

Please excuse my ignorance in this area.

Kind regards
Trevor

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: How to locate a string within XML tags

Post by Marcus Tettmar » Tue Aug 11, 2015 1:48 pm

XMLParse works against a string not a file.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Trevor Hughes
Junior Coder
Posts: 25
Joined: Sun Dec 15, 2013 9:27 pm

Re: How to locate a string within XML tags

Post by Trevor Hughes » Tue Aug 11, 2015 11:00 pm

I now have the value within the XML tag extracted to the clipboard, thanks to the help provided. The next step is to paste this value with the Authentication tag of another soap script (see below). Can someone help me with this. Doing it manually I would open the other SOAP project, generate the MACValue and copy it, open the second project and paste the generated MACValue between the Authentication tag.

All help would be greatly appreciated.

Code: Select all

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://somewebsite.govt.nz/jbms/msggate/dochdr/v2">
   <soapenv:Header/>
   <soapenv:Body wsu:Id="id-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <v2:DocumentManifest>
         <NumberOfItems>1</NumberOfItems>
         <ManifestItem>
            <Type>DEC</Type>
            <MimeTypeQualifierCode>text/xml</MimeTypeQualifierCode>
            <UniformResourceIdentifier>HERA.xml</UniformResourceIdentifier>
        	<Authentication>xHHAEF25c3V0Pc65TakchJ3nkkdmJ/7ubXKIGl+jrjM=</Authentication>
         </ManifestItem>
      </v2:DocumentManifest>
   </soapenv:Body>
</soapenv:Envelope>

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: How to locate a string within XML tags

Post by hagchr » Wed Aug 12, 2015 5:54 am

Hi, probably better as new topic. However, I only have time to reply now so ... If you have the info in the ClipBoard and just want to replace the section in the string then one alternative could be...

(Go through the string and search for text between <Authentication> and </Authentication>. When found replace it with the contents from the ClipBoard and put the overall result into String1)

Code: Select all

GetClipBoard>tmpClip,0

LabelToVar>tmpString,String0,1,0,{"*/"}

Let>tmp0=(?<=<Authentication>).+?(?=</Authentication>)
RegEx>tmp0,String0,0,m,nm,1,tmpClip,String1

MDL>String1

/*
tmpString:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://somewebsite.govt.nz/jbms/msggate/dochdr/v2">
   <soapenv:Header/>
   <soapenv:Body wsu:Id="id-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <v2:DocumentManifest>
         <NumberOfItems>1</NumberOfItems>
         <ManifestItem>
            <Type>DEC</Type>
            <MimeTypeQualifierCode>text/xml</MimeTypeQualifierCode>
            <UniformResourceIdentifier>HERA.xml</UniformResourceIdentifier>
           <Authentication>xHHAEF25c3V0Pc65TakchJ3nkkdmJ/7ubXKIGl+jrjM=</Authentication>
         </ManifestItem>
      </v2:DocumentManifest>
   </soapenv:Body>
</soapenv:Envelope>
*/

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts