2014-02-26 6 views
0

InDesign에서 XML을 생성 중이며 PHP에서 XML을 구문 분석하고 싶습니다. 다음은 InDesign에서 생성하는 XML의 샘플입니다.PHP로 InDesign에서 생성 된 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> 

XML을 재귀 적으로 구문 분석하기 위해 다음 PHP 코드를 사용하고 있습니다. 나는 데

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 속성 (존재하는 경우)을 보지 않는 한 "About Us"를 읽을 방법이 없습니다. "자동차"와 "자동차"및 "트럭"도 마찬가지입니다.

다시 이것은 InDesign에서 생성 된 XML입니다. 노드에 속성을 추가하도록 설계자에게 요청할 수는 있지만 데이터 입력량을 최소화하려고합니다.

저는 XML이 잘 형성되어 있다고 생각합니다. 어떤 도움이라도 대단히 감사하겠습니다.

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; 
} 

함께 :

답변

1

당신은 노드가 차일이있는 경우, 대체 그를 변경하는 모든 텍스트 값을 무시

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 
관련 문제