PHP解析XML可以利用simplexml_load_string()或simplexml_load_file()來解析
$string = <<Jack Herrington PHP Hacks O'Reilly XML; $xml = simplexml_load_string( $string ); //解析XML字串,simplexml_load_file用來解析xml檔案 foreach( $xml -> book as $book) //用foreach讀取各個book標籤 { echo "author: ".$book -> author." Jack Herrington Podcasting Hacks O'Reilly
"; // author內容 echo "title: ".$book -> title."
"; // title內容 echo "title version屬性: ".$book -> title -> attributes() -> version."
"; // title的version屬性 echo "publisher: ".$book -> publisher."
"; // publisher內容 }
XmlToArray
$xml = simplexml_load_string( $string ); $json = json_encode($xml); $arr = json_decode($json, true);
Categories: PHP