1

I have the following array set in an XML file:

<?xml version="1.0"?>
<hats>
    <a id="Chicago BULLS" img="bulls.jpeg" cost="$25.00" />
    <b id="Toronto RAPTORS" img="raptors.jpeg" cost="$25.00" />
    <c id="Orlando MAGIC" img="magic.jpeg" cost="$25.00" />
</hats>
<clothes>
    <a id="Chicago BULLS 23" img="bullstee.jpeg" cost="$30.00" />
    <b id="Toronto RAPTORS 23" img="torontotee.jpeg" cost="$30.00" />
    <c id="Orlando MAGIC 23" img="magictee.jpeg" cost="$30.00" />
</clothes>

I have used a function I found to convert the XML into a Multidimensional Array which looks like this:

Array
(
    [hats] => Array
        (
            [a] => Array
                (
                )

            [a_attr] => Array
                (
                    [id] => Chicago BULLS
                    [img] => bulls.jpeg
                    [cost] => $25.00
                )

            [b] => Array
                (
                )

            [b_attr] => Array
                (
                    [id] => Toronto RAPTORS
                    [img] => raptors.jpeg
                    [cost] => $25.00
                )

            [c] => Array
                (
                )

            [c_attr] => Array
                (
                    [id] => Orlando MAGIC
                    [img] => magic.jpeg
                    [cost] => $25.00
                )

        )

)

As you can see it has only taken half of the XML file, and also I am unable to use numbers so have to resort to letters.

Ultimately I am trying to have it read the XML file then output only what is shown within the tags and tags and eventually more.

How can I parse the XML file cleanly into an array so I can then use a foreach loop to show each line in a given section of the XML file (hats only or clothes only).

1
  • @NathanSrivi I don't have the php written up yet but it will be something like: foreach $array->hats $out ( echo $out['id'] ) With more HTML formatting to show each item individually in its own <div> Hope this makes sense. Commented Nov 16, 2013 at 4:54

2 Answers 2

1

Why not read directly from XML with simplexml?

$xml = simplexml_load_string($x); // assume XML in $x

foreach ($xml->children() as $name => $value) {
    echo "$name: <br />"; // name of the node, e.g. '<hats>'
    foreach ($value->children() as $item) {
        echo "$item[id], $item[img], $item[cost]<br />";
    }
}

see this working: http://3v4l.org/dpjsg

In case you are the creator of this XML, make it easier to parse like this:

<items>
    <group name="hats">
        <item id="1" name="Chicago BULLS" img="bulls.jpeg" cost="25.00" />
    </group>
    <group name="clothes">
        ...
    </group>
</items>
Sign up to request clarification or add additional context in comments.

4 Comments

This is definitely a good approach, but your access for attributes really should quote the strings, not rely on bare words (undefined constants): $item['id'] not $item[id].
Also, I'm not sure where exactly you're going with the "easier to parse" example - you seem to have exchanged specifically named elements for calling everything group...
The tags 'hats' and 'clothes' will only be used as separators for the inside elements, these main elements will not contain values or be directly referenced.
@IMSoP easier to parse: (1) separating structure (=group) from content (=hats), (2) e.g. selecting all items with xpath is easier when items are <items> and not <a>, <b>, <c>, <d> etc.
0

You can use xml_parse_into_struct — Parse XML data into an array structure

Syntax: int xml_parse_into_struct ( resource $parser , string $data , array &$values [, array &$index ] ) This function parses an XML string into 2 parallel array structures, one (index) containing pointers to the location of the appropriate values in the values array. These last two parameters must be passed by reference.

 <?php
    $simple = '<hats>
                <a id="Chicago BULLS" img="bulls.jpeg" cost="$25.00" />
                <b id="Toronto RAPTORS" img="raptors.jpeg" cost="$25.00" />
                <c id="Orlando MAGIC" img="magic.jpeg" cost="$25.00" />
            </hats>
            <clothes>
                <a id="Chicago BULLS 23" img="bullstee.jpeg" cost="$30.00" />
                <b id="Toronto RAPTORS 23" img="torontotee.jpeg" cost="$30.00" />
                <c id="Orlando MAGIC 23" img="magictee.jpeg" cost="$30.00" />
            </clothes>';
    $p = xml_parser_create();
    xml_parse_into_struct($p, $simple, $vals, $index);
    xml_parser_free($p);
    echo "Index array\n";
    print_r($index);
    echo "\nVals array\n";
    print_r($vals);
  ?>

Ref: https://www.php.net/xml_parse_into_struct

2 Comments

Writing code which reads from the struct returned by that function ends up much like writing a custom parser using xml_parse; there are generally better options if you just want to access a basic XML structure.
I have given this a go and it has made an array, but I am unable to figure out how the array works let alone how to easily call on it. I had a look at the link you posted and further down it talks about a function 'AminoAcid' as the example used, this gave a much better looking result in the array structure. It is important how the layout of the array is because I wish to call on all items from array->hats and then to call on only items from array->clothes, if that makes sense.

Your Answer

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