XML 解析詳解
範例1 簡單型
1 2 3 4 5 6 | <!--?xml version="1.0" encoding="UTF-8"?--> My Item ABC DEF 2009-02-12T16:53:25Z |
解題
1 2 3 4 5 6 7 8 | $xml_target_path = 'xml1.xml'; $feed = file_get_contents($xml_target_path); $xml = new SimpleXmlElement($feed); foreach ($xml->channel->item as $entry){ $dc = $entry->children('http://purl.org/dc/elements/1.1/'); echo $dc->creator->attributes()->ID; } |
結果
ThisID
範例2 進階型
1 |
解題
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $xml_target_path = 'xml2.xml'; $feed = file_get_contents($xml_target_path); $xml = new SimpleXmlElement($feed); $elementType = $xml->children('uuid:AAAAA-120003-11231')->Schema->children('uuid:AAAAA-120003-11231')->ElementType; foreach( $elementType->children('uuid:AAAAA-120003-11231') as $key ){ $attr = $key->attributes(); $datatype = $key->children('uuid:AAAAA-120003-11231')->datatype->attributes(); echo sprintf(" Column => %s, DataType => %s ", $attr->name, $datatype->{'type'}); } $rs = $xml->children('urn:schemas-microsoft-com:rowset'); foreach( $rs->data->children('#RowsetSchema') as $key){ $attr = $key->attributes(); echo sprintf(" %s => %s ", $attr->StudID, $attr->StudName); } |
結果
Column => StudID, DataType => string
Column => StudName, DataType => string
Column => ClassID, DataType => string
Column => Post, DataType => string
25-0001 => 楊同學
25-0002 => 曾同學
25-0003 => 姚同學
33-0001 => 張同學
33-0003 => 林同學
臉書留言
一般留言