2014-02-20 2 views
0

과 다른 것으로이 내 기본 XML 구조 노드를 넣어SimpleXML을

<Module_Files> 
     <Category name="categorie1"> 
      <file lan="fr> 
       <title> .... </title> 
       <path> .... </path> 
      </file> 
     </Category> 
</Module_Files> 

을하지만

을 할 수 없습니다

이 내 코드 :

$xmlFile->xpath("//Category[@name='" . $categoryName . "']"); //$categoryName = "categorie1" 

$nodeFile = $xmlFile->addChild("file", ""); 
$nodeFile->addAttribute("lang", (string) $data["lang"]); 
$nodeFile->addChild("titre", (string) $data["titre"]); 
$nodeFile->addChild("path", (string) $data["path"]); 

이 코드는 다음과 같이 .. 카테고리 노드 후하지에 파일 노드를 추가 :

<Module_Files> 
    <Category name="categorie1"> 
    </Category> 
    <file lan="fr> 
     <title> .... </title> 
     <path> .... </path> 
     </file> 
</Module_Files> 

어떻게 분류 노드에 파일의 노드를 위해 무엇을 할 수 있는가?

답변

1

xpath 결과를 지정하지 않으므로 하위 요소를 category 요소가 아닌 루트 요소에 추가하십시오. 사용해보기 :

$categories = $xmlFile->xpath("//Category[@name='" . $categoryName . "']"); //$categoryName = "categorie1" 

if (isset($categories[0])) { 
    $nodeFile = $categories[0]->addChild("file", ""); 
    $nodeFile->addAttribute("lang", (string) $data["lang"]); 
    $nodeFile->addChild("titre", (string) $data["titre"]); 
    $nodeFile->addChild("path", (string) $data["path"]); 
} else { 
    echo 'Not found Category node with name: '.$categoryName.PHP_EOL; 
} 
+0

감사합니다 :) – DOZ