这是我的示例xml文件。我怎样才能获得每个元素,属性及其价值。我想要所有版本的代码和名称,所有版本的代码和名称以及所有品牌的代码和名称?
<?xml version="1.0" encoding="UTF-8"?>
<PLM_University namespace="DS">
<ALL_VERSIONS>
<version code="1" name="V1" description="Version for CLS Products">
<RELEASES_LIST>
<release code="7" name=".7" rank="5" description="">
<BRAND_LIST>
<BRAND code="WLS" name="Companion">
<SOLUTIONS_LIST>
<SOLUTION code="WLS_STU" name="Companion Learning Space" />
</SOLUTIONS_LIST>
</BRAND>
</BRAND_LIST>
</release>
</RELEASES_LIST>
</version>
<version code="10" name="Matrix10" description="Version for Matrix Products">
<RELEASES_LIST>
<release code="6" name=".6" rank="1" description="">
<BRAND_LIST>
<BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation">
<SOLUTIONS_LIST>
<SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" />
</SOLUTIONS_LIST>
</BRAND>
</BRAND_LIST>
</release>
<release code="7" name=".7" rank="2" description="">
<BRAND_LIST>
<BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation">
<SOLUTIONS_LIST>
<SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" />
</SOLUTIONS_LIST>
</BRAND>
</BRAND_LIST>
</release>
<release code="8" name=".8" rank="3" description="">
<BRAND_LIST>
<BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation">
<SOLUTIONS_LIST>
<SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" />
</SOLUTIONS_LIST>
</BRAND>
</BRAND_LIST>
</release>
</RELEASES_LIST>
</version>
</ALL_VERSIONS>
</PLM_University>
答案 0 :(得分:0)
这是一个可以使用的简单解决方案:
<?php
//Load xml file and than json encode/decode it into an array.
$a = json_decode(json_encode((array) simplexml_load_file('path_to_your_xml.xml'),1);
//Loop the array creating a return array
$return = array();
foreach($a['ALL_VERSIONS']['version'] as $key=>$value){
$return[$key]['code'] = $value['@attributes']['code'];
$return[$key]['name'] = $value['@attributes']['name'];
if(is_array($value['RELEASES_LIST']['release']) && isset($value['RELEASES_LIST']['release'][0]['BRAND_LIST']['BRAND']['@attributes']['code'])){
//Loop sup versions
foreach($value['RELEASES_LIST']['release'] as $key2=>$sub){
$return[$key][$key2]['RELEASES_LIST'] = array(
'core'=>$sub['@attributes']['code'],
'name'=>$sub['@attributes']['name']);
$return[$key][$key2]['BRAND_LIST'] = array(
'brand'=>$sub['BRAND_LIST']['BRAND']['@attributes']['code'],
'brand_name'=>$sub['BRAND_LIST']['BRAND']['@attributes']['name']);
}
}else{
$return[$key]['RELEASES_LIST'] = array(
'core'=>$value['RELEASES_LIST']['release']['@attributes']['code'],
'name'=>$value['RELEASES_LIST']['release']['@attributes']['name']);
$return[$key]['BRAND_LIST'] = array(
'brand'=>$value['RELEASES_LIST']['release']['BRAND_LIST']['BRAND']['@attributes']['code'],
'brand_name'=>$value['RELEASES_LIST']['release']['BRAND_LIST']['BRAND']['@attributes']['name']);
}
}
print_r($return);
/*
Array
(
[0] => Array
(
[code] => 1
[name] => V1
[RELEASES_LIST] => Array
(
[core] => 7
[name] => .7
)
[BRAND_LIST] => Array
(
[brand] => WLS
[brand_name] => Companion
)
)
[1] => Array
(
[code] => 10
[name] => Matrix10
[0] => Array
(
[RELEASES_LIST] => Array
(
[core] => 6
[name] => .6
)
[BRAND_LIST] => Array
(
[brand] => ENOVIA
[brand_name] => ENOVIA Collaborative Innovation
)
)
[1] => Array
(
[RELEASES_LIST] => Array
(
[core] => 7
[name] => .7
)
[BRAND_LIST] => Array
(
[brand] => ENOVIA
[brand_name] => ENOVIA Collaborative Innovation
)
)
[2] => Array
(
[RELEASES_LIST] => Array
(
[core] => 8
[name] => .8
)
[BRAND_LIST] => Array
(
[brand] => ENOVIA
[brand_name] => ENOVIA Collaborative Innovation
)
)
)
)
*/
?>
答案 1 :(得分:0)
$xml = simplexml_load_file("data.xml"); foreach($xml->ALL_VERSIONS->version as $child) { echo "Version code : ".$child['code']; echo "Version name : ".$child['name']; foreach($child->RELEASES_LIST->release as $releaseChild) { echo "release code : ".$releaseChild['code']; echo "release name : ".$releaseChild['name']; echo "brand code : ".$releaseChild->BRAND_LIST->BRAND['code']; echo "brand name : ".$releaseChild->BRAND_LIST->BRAND['name']; } }
答案 2 :(得分:-1)
在php中你可以使用DOMDocument
http://php.net/manual/en/class.domdocument.php
在php上
例如:
<?php
$handle = fopen("1.xml", "rb");
$xml= '';
while (!feof($handle)) {
$xml .= fread($handle, 8192);
}
fclose($handle);
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}
?>
请参阅此页面以获取更多示例http://www.php.net/manual/en/domdocument.getelementsbytagname.php