如何从http://www.newyorkfed.org/xml/data/fx/ATSNoon.xml解析此XML文件

时间:2015-11-08 08:44:35

标签: php xml parsing

我有以下xml文件http://www.newyorkfed.org/xml/data/fx/ATSNoon.xml,我必须使用PHP解析它。实际上我需要解析所有和数组。我需要的数组将是:

$rates = (array('date' => '1994-01-06', 'value' => '12.2430'), 
      array('date' => '1994-01-07', 'value' => '13.2430'),
      array(...)
);

在解析XML文件方面经验不佳,无论我尝试什么,我都无法成功完成此任务。我可以请一些帮助吗? 谢谢

2 个答案:

答案 0 :(得分:1)

如果为元素的命名空间注册了前缀,可以使用Xpath获取它们:

$document = new DOMDocument();
$document->load($xmlFile);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('f', 'http://www.newyorkfed.org/xml/schemas/FX/utility');

$result =[];
foreach ($xpath->evaluate('//f:Obs') as $obs) {
  $result[] = [
    'date' => $xpath->evaluate('string(f:TIME_PERIOD)', $obs),
    'value' => $xpath->evaluate('string(f:OBS_VALUE)', $obs)
  ]; 
}
var_dump($result);

输出:

array(??) {
  [0]=>
  array(2) {
    ["date"]=>
    string(10) "1994-01-06"
    ["value"]=>
    string(7) "12.2430"
  }
  [1]=>
  array(2) {
    ["date"]=>
    string(10) "1994-01-07"
    ["value"]=>
    string(7) "12.2190"
  }
  [2]=>
  array(2) {
    ["date"]=>
    string(10) "1994-01-10"
    ["value"]=>
    string(7) "12.20
  ...

答案 1 :(得分:0)

使用以下代码,您应该能够根据您的要求操作输出..

/* 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;
相关问题