2015-11-08 20 views
0

我有以下xml文件http://www.newyorkfed.org/xml/data/fx/ATSNoon.xml,我必須使用PHP解析它。事實上,我需要解析所有的和數組。我需要的陣列將是這樣的:如何從http://www.newyorkfed.org/xml/data/fx/ATSNoon.xml解析此XML文件

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

經與解析XML文件的一個糟糕的體驗,我無法完成這個任務無論怎樣我嘗試成功。我可以請求幫助嗎? 謝謝

+0

可以使用一個XML解析器或用於該S pecific案件[看到一個正則表達式的想法](https://eval.in/464925)。 –

+0

感謝bobble泡泡......它完美地與您的解決方案配合使用。 – user10851

+0

不客氣@ user10851! –

回答

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 
    ... 
+0

謝謝。這項技術非常好。非常感謝你。 – user10851

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;