这是名为test.xml的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>
我用
加载它$xml = simplexml_load_file("test.xml");
我从这里link
尝试了大部分信息但没有成功。我想创建一些数组,然后我将自定义它。 我也尝试了xml_parse_into_struct(),但没有成功。
答案 0 :(得分:1)
尝试......
$doc = simplexml_load_file('t1.xml');
foreach ( $doc->shopproduct as $shopproduct) {
echo $shopproduct->product_id."=".$shopproduct->name."\n";
}
这将显示一些数据,可能有助于你想要对数组做什么。
答案 1 :(得分:0)
来自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;
}