Sign in to follow this  
Followers 0
Geir1983

StringExplode xml

5 posts in this topic

#1 ·  Posted (edited)

Hi

I am trying to split up my xml string into an array, using delimiter "<Target>". However when using this as delimiter to the _StringExplode it will split on every characther(?), not the complete string. What am i doing wrong? I know this could be done with RegExp, but i am not so familiar with this so i prefer to do it with _StringExplode

I expect results like this:

$arrBroadcast[0] = "<Name>CP-19F8B0</Name><NetId>5.25.248.176.1.1</NetId><IpAddr>10.80.1.11</IpAddr><Version>3.1.4012</Version><OS>Win CE (7.0)</OS></Target>"

$arrBroadcast[1] = "<Name>CP_03D225</Name><NetId>5.3.210.37.1.1</NetId><IpAddr>10.80.1.26</IpAddr><Version>2.10.1342</Version><OS>Win CE (5.0)</OS></Target>"

My code:

#include <Array.au3>
#include <String.au3>

$InputString = "<BroadcastSearch>1</BroadcastSearch><Target><Name>CP-19F8B0</Name><NetId>5.25.248.176.1.1</NetId><IpAddr>10.80.1.11</IpAddr><Version>3.1.4012</Version><OS>Win CE (7.0)</OS></Target><Target><Name>CP_03D225</Name><NetId>5.3.210.37.1.1</NetId><IpAddr>10.80.1.26</IpAddr><Version>2.10.1342</Version><OS>Win CE (5.0)</OS></Target><Target><Name>CX-0FED5A</Name><NetId>5.12.242.252.1.1</NetId><IpAddr>192.168.0.110</IpAddr><Version>3.1.4009</Version><OS>Win XP</OS></Target><Target><Name>CX-128D3F</Name><NetId>5.18.141.103.1.1</NetId><IpAddr>192.168.0.59</IpAddr><Version>2.11.2241</Version><OS>Win CE (7.0)</OS></Target><Target><Name>Stensarmen16_DL</Name><NetId>5.20.137.91.1.1</NetId><IpAddr>10.80.1.14</IpAddr><Version>2.11.2233</Version><OS>Win CE (6.0)</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>192.168.0.106</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>10.80.1.21</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target>"
$arrBroadcast = _StringExplode($InputString, "<Target>")
_ArrayDisplay($arrBroadcast)

According to the documentation on _StringExplode (am i reading this wrong?):

Parameters $sString String to be split $sDelimiter Delimiter to split on (split is performed on entire string, not individual characters)

 

Edit:

I can do it with _StringBetween, but should it also work with _StringExplode?

$InputString = "<BroadcastSearch>1</BroadcastSearch><Target><Name>CP-19F8B0</Name><NetId>5.25.248.176.1.1</NetId><IpAddr>10.80.1.11</IpAddr><Version>3.1.4012</Version><OS>Win CE (7.0)</OS></Target><Target><Name>CP_03D225</Name><NetId>5.3.210.37.1.1</NetId><IpAddr>10.80.1.26</IpAddr><Version>2.10.1342</Version><OS>Win CE (5.0)</OS></Target><Target><Name>CX-0FED5A</Name><NetId>5.12.242.252.1.1</NetId><IpAddr>192.168.0.110</IpAddr><Version>3.1.4009</Version><OS>Win XP</OS></Target><Target><Name>CX-128D3F</Name><NetId>5.18.141.103.1.1</NetId><IpAddr>192.168.0.59</IpAddr><Version>2.11.2241</Version><OS>Win CE (7.0)</OS></Target><Target><Name>Stensarmen16_DL</Name><NetId>5.20.137.91.1.1</NetId><IpAddr>10.80.1.14</IpAddr><Version>2.11.2233</Version><OS>Win CE (6.0)</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>192.168.0.106</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>10.80.1.21</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target>"
$arrBroadcast = _StringBetween($InputString, "<Target>", "</Target>")
_ArrayDisplay($arrBroadcast)
Edited by Geir1983

Share this post


Link to post
Share on other sites



Geir1983,

How about this...

#include <Array.au3>
#include <String.au3>

$InputString = "<BroadcastSearch>1</BroadcastSearch><Target><Name>CP-19F8B0</Name><NetId>5.25.248.176.1.1</NetId><IpAddr>10.80.1.11</IpAddr><Version>3.1.4012</Version><OS>Win CE (7.0)</OS></Target><Target><Name>CP_03D225</Name><NetId>5.3.210.37.1.1</NetId><IpAddr>10.80.1.26</IpAddr><Version>2.10.1342</Version><OS>Win CE (5.0)</OS></Target><Target><Name>CX-0FED5A</Name><NetId>5.12.242.252.1.1</NetId><IpAddr>192.168.0.110</IpAddr><Version>3.1.4009</Version><OS>Win XP</OS></Target><Target><Name>CX-128D3F</Name><NetId>5.18.141.103.1.1</NetId><IpAddr>192.168.0.59</IpAddr><Version>2.11.2241</Version><OS>Win CE (7.0)</OS></Target><Target><Name>Stensarmen16_DL</Name><NetId>5.20.137.91.1.1</NetId><IpAddr>10.80.1.14</IpAddr><Version>2.11.2233</Version><OS>Win CE (6.0)</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>192.168.0.106</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>10.80.1.21</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target>"
$arrBroadcast = stringsplit($InputString, "<Target>",3)
_ArrayDisplay($arrBroadcast)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

There's a trouble with _StringExplode

In 3.3.8.1 the return was

Return StringSplit($sString, $sDelimiter, 3)  ; that is $STR_NOCOUNT + $STR_ENTIRESPLIT

But in the 3.3.12.0 it is

Return StringSplit($sString, $sDelimiter, $STR_NOCOUNT)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Like this ( i had to add in a root node, since your xml was not valid):

$InputString = "<BroadcastSearch>1</BroadcastSearch><Target><Name>CP-19F8B0</Name><NetId>5.25.248.176.1.1</NetId><IpAddr>10.80.1.11</IpAddr><Version>3.1.4012</Version><OS>Win CE (7.0)</OS></Target><Target><Name>CP_03D225</Name><NetId>5.3.210.37.1.1</NetId><IpAddr>10.80.1.26</IpAddr><Version>2.10.1342</Version><OS>Win CE (5.0)</OS></Target><Target><Name>CX-0FED5A</Name><NetId>5.12.242.252.1.1</NetId><IpAddr>192.168.0.110</IpAddr><Version>3.1.4009</Version><OS>Win XP</OS></Target><Target><Name>CX-128D3F</Name><NetId>5.18.141.103.1.1</NetId><IpAddr>192.168.0.59</IpAddr><Version>2.11.2241</Version><OS>Win CE (7.0)</OS></Target><Target><Name>Stensarmen16_DL</Name><NetId>5.20.137.91.1.1</NetId><IpAddr>10.80.1.14</IpAddr><Version>2.11.2233</Version><OS>Win CE (6.0)</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>192.168.0.106</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target><Target><Name>StianA-nb1</Name><NetId>10.80.1.21.1.1</NetId><IpAddr>10.80.1.21</IpAddr><Version>3.1.4016</Version><OS>Windows 7</OS></Target>"
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml("<root>" & $InputString & "</root>")

$oTargets = $oXML.SelectNodes('//Target')
For $oTarget In $oTargets
    $oName = $oTarget.SelectSingleNode('./Name')
    $oNetID = $oTarget.SelectSingleNode('./NetId')
    $oVersion = $oTarget.SelectSingleNode('./Version')
    $oOS = $oTarget.SelectSingleNode('./OS')
    ConsoleWrite("$oName=[" & $oName.text & "] $oNetID=[" & $oNetID.text & "] $oVersion=[" & $oVersion.text & "] $oOS=[" & $oOS.text & "]" & @CRLF )
Next

output:

$oName=[CP-19F8B0] $oNetID=[5.25.248.176.1.1] $oVersion=[3.1.4012] $oOS=[Win CE (7.0)]
$oName=[CP_03D225] $oNetID=[5.3.210.37.1.1] $oVersion=[2.10.1342] $oOS=[Win CE (5.0)]
$oName=[CX-0FED5A] $oNetID=[5.12.242.252.1.1] $oVersion=[3.1.4009] $oOS=[Win XP]
$oName=[CX-128D3F] $oNetID=[5.18.141.103.1.1] $oVersion=[2.11.2241] $oOS=[Win CE (7.0)]
$oName=[stensarmen16_DL] $oNetID=[5.20.137.91.1.1] $oVersion=[2.11.2233] $oOS=[Win CE (6.0)]
$oName=[stianA-nb1] $oNetID=[10.80.1.21.1.1] $oVersion=[3.1.4016] $oOS=[Windows 7]
$oName=[stianA-nb1] $oNetID=[10.80.1.21.1.1] $oVersion=[3.1.4016] $oOS=[Windows 7]

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0