cancel
Showing results for 
Search instead for 
Did you mean: 

iterate through XML Elements (or convert to array)

SOLVED
jochenmaurer
Contributor

iterate through XML Elements (or convert to array)

Hi,

does anybody know a easy way to iterate through xml-elements within trafficscript?

my xml (-part) looks like this:


<member>


<name>title</name>


<value>


<string>this is the title</string>


</value>


</member>


<member>


<name>description</name>


<value>


<string>this is the description</string>


</value>


</member>


and i want to iterate through the "member"-sections... It would be great if there is a xml-toarray or xml-tohash function.

-Jochen

1 ACCEPTED SOLUTION

Accepted Solutions
aannavarapu
Contributor

Re: iterate through XML Elements (or convert to array)

Hi Jochen,

Use the following code snippet to get the response. Note that I wrapped your xml example to a root element to avoid xml parsing errors.

$count = xml.xpath.matchNodeCount($body,"","//root/member");

$response = "There are " . $count . " members\n";

for( $i=1; $i <= $count; $i++ ) {

   $query = "//root/member[" . $i. "]/name/text()";

    $name = xml.xpath.matchNodeSet($body,"",$query );

   $query = "//root/member[" . $i . "]/value/string/text()";

    $value = xml.xpath.matchNodeSet($body,"",$query );

   $response .= "  " . $name . ": " . $value . "\n";

}


Regards,

Arun

View solution in original post

1 REPLY 1
aannavarapu
Contributor

Re: iterate through XML Elements (or convert to array)

Hi Jochen,

Use the following code snippet to get the response. Note that I wrapped your xml example to a root element to avoid xml parsing errors.

$count = xml.xpath.matchNodeCount($body,"","//root/member");

$response = "There are " . $count . " members\n";

for( $i=1; $i <= $count; $i++ ) {

   $query = "//root/member[" . $i. "]/name/text()";

    $name = xml.xpath.matchNodeSet($body,"",$query );

   $query = "//root/member[" . $i . "]/value/string/text()";

    $value = xml.xpath.matchNodeSet($body,"",$query );

   $response .= "  " . $name . ": " . $value . "\n";

}


Regards,

Arun