0

I have a problem parsing external XML files to PHP array. Structure of XML file is like that:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">
<svg style="shape-rendering:geometricPrecision;" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid meet" viewBox="0 0 530 900" x="0px" y="0px" width="530px" height="900px"> 
    <g font-family="'sans-serif"> 
        <text x="0" y="76" font-size="14">text content</text>
        <text x="0" y="76" font-size="14">text content</text>
        <text x="0" y="76" font-size="14">text content</text>
        <text x="0" y="76" font-size="14">text content</text>
        <rect width="530" height="900" x="0" y="0" fill="white" fill-opacity="0" />
    </g>
</svg>

I'm trying to get array of "text" elements like:

> Array (
>     [0] => text content
>     [1] => text content
>     [2] => text content
>     [3] => text content )

I've tried few different ways but of some reason I have a problem to access to elements I want. The only working solution I found was:

$xmlstring = file_get_contents("xmlfile.php?ID=someId");
$xml = new simpleXml2Array( $xmlstring, null );
$xmlarray = $xml->arr[g][0][content][text];

$values = array();
for( $i= 0 ; $i < count($xmlarray) ; $i++ ) {
        $values[] = $xmlarray[$i][content];
}

print_r( $values );

It's using "simpleXml2Array" class but I'd like to avoid it and get values I want using foreach loop. I'm looking for the most simple and easy solution.

3 Answers 3

1

A possible solution would be to use simplexml_load_string.

That will return an object of class SimpleXMLElement. Then you can use its properties to get the xml data.

To get the text from the text elements into your $values array you can use a foreach loop:

$xmlstring = file_get_contents("xmlfile.php?ID=someId");
$xml = simplexml_load_string($xmlstring);

$values = array();

foreach ($xml->g->text as $text) {
    $values[] = $text->__toString();
}

The $text variable in the foreach loop is of type SimpleXMLElement and you can use its __toString() method to get the text content.

Sign up to request clarification or add additional context in comments.

1 Comment

I had a problem to loop correctly over results but using __toString() seems to be grat solution. Thank you for help!
1

There's a trick you can do using json_encode/decode to convert and XML object into an array, try this:

$temp = simplexml_load_string( trim( $xmlstring ), "SimpleXMLElement", LIBXML_NOCDATA );
$data = json_decode( json_encode( $temp ), true );
print_r( $data );

The data you're looking for would be located in:

$data['g']['text']

Comments

0

Use following function

$source_xml = simplexml_load_file($xmlUrl);

and use it in foreach loop

1 Comment

I'm failing creating loop for this one: <code>$xmlarray = array(); foreach ($xml->g as $g) { foreach ($g->text as $text) { $xmlarray[] = $text } } print_r($xmlarray);</code>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.