2012-10-22 3 views
1

PHP SimpleXML로 구문 분석하는 XML이 있습니다. 내 XML 파일의 일부는 다음과 같습니다XML로 PHP 구문 분석 - SimpleXML

Parsing 'deviceStatusPolling'... 
    Has 1 attribute(s): 
    - interval: 60 

그것은 아이들을 구문 분석하지 않습니다 :

<r: datapoint programmaticName="t_val_sys_pct_mssDbPartitionUsage" /> 

이이

<deviceStatusPolling interval="60"> 
    <r:datapoint programmaticName="t_val_sys_pct_mssDbPartitionUsage" /> 
</deviceStatusPolling> 

내가 그 구문 분석 내가 무엇을 얻을 구문 분석 함수 :

function parse_recursive(SimpleXMLElement $element, $level = 0) { 
$indent = str_repeat("\t", $level); // determine how much we'll indent 

$value = trim((string) $element); // get the value and trim any whitespace from the start and end 
$attributes = $element->attributes(); // get all attributes 
$children = $element->children();  // get all children 

echo "{$indent}Parsing '{$element->getName()}'...<br>"; 
if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children 
{ 
    echo "{$indent}Value: {$element}<br>"; 
} 

// only show attributes if there are any 
if(count($attributes) > 0) 
{ 
    echo $indent.'Has '.count($attributes).' attribute(s):<br>'; 
    foreach($attributes as $attribute) 
    { 
     echo "{$indent}- {$attribute->getName()}: {$attribute}<br>"; 
    } 
} 

// only show children if there are any 
if(count($children)) 
{ 
    echo $indent.'Has '.count($children).' child(ren):<br>'; 
    foreach($children as $child) 
    { 
     parse_recursive($child, $level+1); // recursion :) 
    } 
} 

echo $indent; // just to make it "cleaner" 
echo "<br>"; 
} 

SimpleXML의 한계점이 있습니까? 아니면 내가 잘못하고있는거야?

감사

+2

SO! 우리에게 당신의 코드를 보여주십시오. – nickhar

+0

도 인쇄 할 때 어떤 점이 좋을지 알려주세요. – Serge

+0

인쇄하기 위해 무엇을하고 있는지 여전히 볼 수 없습니다 - 어떤 코드입니까? 인쇄? – nickhar

답변

0

귀하의 질문은 매우 명확하지 않다, 그러나 아마 이런 식으로 뭔가가 필요합니다

$xmlDoc = new SimpleXMLElement($docStr); 
$allNamespaces = $xmlDoc->getNamespaces(true); 
foreach ($allNamespaces as $nspace => $nurl) { 
     $xmlDoc->registerXPathNamespace($nspace, $nurl); 
} 

SimpleXML을하고 네임 스페이스에 최대 읽기.

이것은 거의 확실하게 문제의 해결책입니다. 여기를 참조하십시오 : http://blog.sherifmansour.com/?p=302

+0

시도했지만 문제가 해결되지 않았습니다. – alereis

+0

추가 정보 : http://blog.sherifmansour.com/?p=302 –