0

This is the Xml File with name test.xml:

<shop>
    <date_created>2017-06-26 15:20:37</date_created>
    <shopproduct>
        <product_id>30</product_id>
        <name>Canon EOS 5D</name>
        <model>Product 3</model>
    </shopproduct>
    <shopproduct>
        <product_id>47</product_id>
        <name>HP LP3065</name>
        <model>Product 21</model>
    </shopproduct>
    <shopproduct>
        <product_id>28</product_id>
        <name>HTC Touch HD</name>
        <model>Product 1</model>
    </shopproduct>
    <shopproduct>
        <product_id>41</product_id>
        <name>iMac</name>
        <model>Product 14</model>
    </shopproduct>
    <shopproduct>
        <product_id>40</product_id>
        <name>iPhone</name>
        <model>product 11</model>
    </shopproduct>
    <shopproduct>
        <product_id>48</product_id>
        <name>iPod Classic</name>
        <model>product 20</model>
    </shopproduct>
    <shopproduct>
        <product_id>36</product_id>
        <name>iPod Nano</name>
        <model>Product 9</model>
    </shopproduct>
    <shopproduct>
        <product_id>34</product_id>
        <name>iPod Shuffle</name>
        <model>Product 7</model>
    </shopproduct>
    <shopproduct>
        <product_id>32</product_id>
        <name>iPod Touch</name>
        <model>Product 5</model>
    </shopproduct>
    <shopproduct>
        <product_id>43</product_id>
        <name>MacBook</name>
        <model>Product 16</model>
    </shopproduct>
    <shopproduct>
        <product_id>44</product_id>
        <name>MacBook Air</name>
        <model>Product 17</model>
    </shopproduct>
    <shopproduct>
        <product_id>45</product_id>
        <name>MacBook Pro</name>
        <model>Product 18</model>
    </shopproduct>
    <shopproduct>
        <product_id>31</product_id>
        <name>Nikon D300</name>
        <model>Product 4</model>
    </shopproduct>
    <shopproduct>
        <product_id>29</product_id>
        <name>Palm Treo Pro</name>
        <model>Product 2</model>
    </shopproduct>
    <shopproduct>
        <product_id>35</product_id>
        <name>Product 8</name>
        <model>Product 8</model>
    </shopproduct>
    <shopproduct>
        <product_id>49</product_id>
        <name>Samsung Galaxy Tab 10.1</name>
        <model>SAM1</model>
    </shopproduct>
    <shopproduct>
        <product_id>33</product_id>
        <name>Samsung SyncMaster 941BW</name>
        <model>Product 6</model>
    </shopproduct>
    <shopproduct>
        <product_id>46</product_id>
        <name>Sony VAIO</name>
        <model>Product 19</model>
    </shopproduct>
</shop>

I load it with

$xml = simplexml_load_file("test.xml");

I tried most info from here link

But without success. I want to create some array, then i will customize it. Also i tried xml_parse_into_struct(), again without success.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46

2 Answers2

1

Try ...

$doc = simplexml_load_file('t1.xml');

foreach ( $doc->shopproduct as $shopproduct)   {
    echo $shopproduct->product_id."=".$shopproduct->name."\n";
}

This will show some of the data, perhaps help in what you want to do with the array.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

from http://php.net/ref.simplexml:

function xml2array ( $xmlObject, $out = array () )
{
    foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) || is_array ( $node ) ) ? xml2array ( $node ) : $node;

    return $out;
}
dWinder
  • 11,597
  • 3
  • 24
  • 39