與下面的代碼,你應該能夠操縱輸出到您的要求..
/* utility class to simplify working with DOMDocument & XML */
class xmldom{
private $xml;
private $parse_errs;
public function __construct($data, $options=array()){
try{
$opts=(object)array_merge(array(
'encoding' => 'utf-8',
'validate' => false,
'standalone' => true,
'preserve' => true,
'strict' => false,
'substitute' => false,
'recover' => true,
'format' => false
),$options);
libxml_use_internal_errors(TRUE);
$this->xml = new DOMDocument('1.0',$opts->encoding);
$this->xml->validateOnParse=$opts->validate;
$this->xml->standalone=$opts->standalone;
$this->xml->preserveWhiteSpace=$opts->preserve;
$this->xml->strictErrorChecking=$opts->strict;
$this->xml->substituteEntities=$opts->substitute;
$this->xml->recover=$opts->recover;
$this->xml->formatOutput=$opts->format;
$this->xml->loadXML(mb_convert_encoding($data, $opts->encoding));
$this->parse_errs=serialize(libxml_get_last_error());
libxml_clear_errors();
}catch(Exception $e){
die($e->getMessage());
}
}
public function getdom(){
return $this->xml;
}
public function geterrors(){
return $this->parse_errs;
}
}
/* Utility class to iterate nodes in the DOM */
class RecursiveDOMIterator implements RecursiveIterator {
private $index;
private $list;
public function __construct(DOMNode $domNode){
$this->index = 0;
$this->list = $domNode->childNodes;
}
public function current(){
return $this->list->item($this->index);
}
public function getChildren(){
return new self($this->current());
}
public function hasChildren(){
return $this->current()->hasChildNodes();
}
public function key(){
return $this->index;
}
public function next(){
$this->index++;
}
public function rewind(){
$this->index = 0;
}
public function valid(){
return $this->index < $this->list->length;
}
}
/* Get the xml data */
$url=file_get_contents('http://www.newyorkfed.org/xml/data/fx/ATSNoon.xml');
/* Construct DOMDocument object using xml data as source */
$dom=new xmldom( $url);
$xml=$dom->getdom();
/* Find the Root node in the DOMDocument */
$rootnode=$xml->getElementsByTagName('UtilityData')->item(0);
/* Create the Domnode iterator object */
$nodeItr=new RecursiveDOMIterator($rootnode);
/* Create standard recursive Iterator */
$itr=new RecursiveIteratorIterator($nodeItr, RecursiveIteratorIterator::SELF_FIRST);
/* Iterate through the DOM and echo out tagname & value */
foreach($itr as $node) {
if($node->nodeType === XML_ELEMENT_NODE) {
/*
Here you can add to an array or manipulate however you see fit.
*/
echo $node->nodeName . ' ' . $node->nodeValue. '<br />';
}
}
/* clean up */
$dom=$rootnode=$itr=$nodeItr=null;
可以使用一個XML解析器或用於該S pecific案件[看到一個正則表達式的想法](https://eval.in/464925)。 –
感謝bobble泡泡......它完美地與您的解決方案配合使用。 – user10851
不客氣@ user10851! –