2014-10-01 2 views
1

XML 파일이있어서 가치를 얻으려고합니다. 변수 media_id에서 12345 값이 필요합니다. 어떻게 PHP와 simplexml로 얻을 수 있습니까?simplexml로 값을 얻는 방법?

<?xml version="1.0" encoding="UTF-8"?> 
<Playerdata> 
    <Clip> 
     <MediaType>video_episode</MediaType> 
     <Duration>5400</Duration> 
     <PassthroughVariables> 
      <variable name="media_type" value="video_episode"/> 
      <variable name="media_id" value="12345"/> 
     </PassthroughVariables> 
    </Clip> 
</Playerdata> 

나는 이제이 있습니다

$xml = simplexml_load_file("file.xml"); 

답변

0

이 시도 :

여기
$xml = simplexml_load_file("file.xml"); 
$variable = $xml->xpath('//variable[@name="media_id"]')[0]; 
echo $variable["value"]; 
0

당신은 그것을 분석하고 SimpleXML을 객체를 반환합니다 SimpleXML을에 XML 파일을로드 할 수 있습니다.

$xml = simplexml_load_file('path/to/file.xml'); 
//then you should be able to access the data through objects 
$passthrough = $xml->Clip->PassthroughVariables; 
//because you have many children in the PassthroughVariables you'll need to iterate 
foreach($passthrough as $p){ 
    //to get the attributes of each node you'll have to call attributes() on the object 
    $attributes = $p->attributes(); 
    //now we can iterate over each attribute 
    foreach($attributes as $a){ 
     //SimpleXML will assume each data type is a SimpleXMLElement/Node 
     //so we need to cast it for comparisons 
     if((String)$a->name == "media_id"){ 
      return (int)$a->value; 
     } 
    } 
} 

SimpleXMLObject로 작업 할 때는 SimpleXMLElement 설명서가 좋습니다. http://uk1.php.net/manual/en/class.simplexmlelement.php

0

승입니다/

$xml = simplexml_load_file('file.xml'); 
$value = (int) $xml->Clip->PassthroughVariables->variable[1]['value']; 
XPath는 O