XML 解析詳解

範例1 簡單型

<!--?xml version="1.0" encoding="UTF-8"?-->
My Item
ABC
DEF
2009-02-12T16:53:25Z

解題


$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 進階型

解題


$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 => 林同學

相連文章

臉書留言

一般留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *