2014-02-26 51 views
0

我從InDesign生成XML並希望在PHP中解析XML。以下是InDesign生成的XML示例:從InDesign與PHP解析生成的XML

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
<page title="About Us"> 
    About Us 
    <page>Overiew</page> 
    <page>Where We Started</page> 
    <page>Help</page> 
</page> 
<page> 
    Automobiles 
    <page> 
    Cars 
    <page>Small</page> 
    <page>Medium</page> 
    <page>Large</page> 
    </page> 
    <page> 
    Trucks 
    <page>Flatbet</page> 
    <page> 
     Pickup 
     <page>Dodge</page> 
     <page>Nissan</page> 
    </page> 
    </page> 
</page> 
</Root> 

我正在使用以下PHP代碼來遞歸地解析XML。

header('Content-type: text/plain'); 

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()}'...".PHP_EOL; 
     if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children 
     { 
       echo "{$indent}Value: {$element}".PHP_EOL; 
     } 

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

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

     echo $indent.PHP_EOL; // just to make it "cleaner" 
} 

$xml = new SimpleXMLElement('data.xml', null, true); 

parse_recursive($xml); 

說我遇到的問題是,當我解析XML,我沒有得到每個頁面節點的文本值,除非由頁面標籤完全包圍。因此,例如,除非查看title屬性(如果存在),否則我無法閱讀「關於我們」。這同樣適用於「汽車」和「汽車」和「卡車」。

同樣,這是從InDesign生成的XML。我可以讓設計人員爲節點添加屬性等,但我試圖儘量減少數據輸入量。

我相信XML格式良好。任何幫助將不勝感激。

回答

1

你忽略所有的文本值,如果節點有任何孩子的,要改變這種替代:

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

if(!empty($value)) // only show value if there is anychildren 
{ 
    echo "{$indent}Value: {$value}".PHP_EOL; 
} 

的然後用樣本數據的結果是:

Parsing 'Root'... 
Has 2 child(ren): 
    Parsing 'page'... 
    Value: About Us 
    Has 1 attribute(s): 
    - title: About Us 
    Has 3 child(ren): 
     Parsing 'page'... 
     Value: Overiew 

     Parsing 'page'... 
     Value: Where We Started 

     Parsing 'page'... 
     Value: Help 


    Parsing 'page'... 
    Value: Automobiles 
    Has 2 child(ren): 
     Parsing 'page'... 
     Value: Cars 
     Has 3 child(ren): 
      Parsing 'page'... 
      Value: Small 

      Parsing 'page'... 
      Value: Medium 

      Parsing 'page'... 
      Value: Large 


     Parsing 'page'... 
     Value: Trucks 
     Has 2 child(ren): 
      Parsing 'page'... 
      Value: Flatbet 

      Parsing 'page'... 
      Value: Pickup 
      Has 2 child(ren): 
       Parsing 'page'... 
       Value: Dodge 

       Parsing 'page'... 
       Value: Nissan