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.

7
  • stackoverflow.com/a/20431742/3568847 Commented Jun 26, 2017 at 12:37
  • yeah i see and try it, but without success Commented Jun 26, 2017 at 12:38
  • Maybe it is because your xml is not in proper format Commented Jun 26, 2017 at 12:39
  • it's check with xml validator, it's valid Commented Jun 26, 2017 at 12:40
  • what do you mean by without success... Have you tried printing that array... do you get an error ???? Commented Jun 26, 2017 at 12:42

2 Answers 2

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.

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

Comments

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;
}

Comments

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.