2012-06-29 4 views
1

DOMDocument를 사용하여 XML을 생성하는데이 XML에는 image-Tag가 있어야합니다. 나는 '이미지'또는 'IMG'에 '이미지'를 변경하면 내가 (간체) 할 어떻게 든DOMDocument를 사용하여 이미지 태그를 만들지 못했습니다.

$response = new DOMDocument(); 
$actions = $response->createElement('actions'); 
$response->appendChild($actions); 

$imageElement = $response->createElement('image'); 
$actions->appendChild($imageElement); 

$anotherNode = $response->createElement('nodexy'); 
$imageElement->appendChild($anotherNode); 

<actions> 
    <img> 
    <node></node> 
</actions> 

결과가 작동합니다. PHP 5.3.10에서 5.3.8로 전환 할 때도 작동합니다.

이것은 버그 또는 기능입니까? 내 생각 엔 DOMDocuments는 HTML img 요소를 만들고 싶다고 가정하고 있습니다 ... 어떻게 든이를 막을 수 있습니까?

이상한 점 : 동일한 서버의 다른 스크립트에서 오류를 재현 할 수 없습니다. http://pastebin.com/KqidsssM

답변

2

2 시간의 비용이 소요되었습니다.

DOMDocument가 XML을 올바르게 렌더링합니다. XML은 ajax 호출에 의해 반환되고 어떤 식 으로든 브라우저/자바 스크립트는 그것을 표시하기 전에 img로 변경합니다 ...

0

이 라인의 $imageAction->getAction() (44 개) 반환 'IMG'가능성이 :하지만 그 오류를 일으키는,

여기 클래스의 전체 페이스트 빈의 ... 패턴을 잡을 수 있습니까? 너 var_dump() 그거 봤니? 어떤 상황에서도 DOM이 "이미지"를 "img"로 변환하는 방법을 알지 못합니다.

+0

예, $ imageAction-> getAction() 정의 된 액션 이름을 반환합니다 (이 경우 "addImage"에서). 그리고 이것은 해석되어 XML에 올바르게 추가됩니다. – shredding

0

"html doc"로 동작한다고 생각합니다. 버전 번호 "1.0"

코드

<?php 

    $response = new DOMDocument('1.0','UTF-8'); 
    $actions = $response->createElement('actions'); 
    $response->appendChild($actions); 

    $imageElement = $response->createElement('image'); 
    $actions->appendChild($imageElement); 

    $anotherNode = $response->createElement('nodexy'); 
    $imageElement->appendChild($anotherNode); 

    echo $response->saveXML(); 

출력 :

<?xml version="1.0" encoding="UTF-8" ?> 
    <actions> 
     <image> 
     <nodexy /> 
     </image> 
    </actions> 

또한 당신이 SimpleXML 클래스

을 사용할 수 있습니다

예 :

<?php 
    $response = new SimpleXMLElement("<actions></actions>"); 
    $imageElement = $response->addChild('image'); 
    $imageElement->addChild("nodexy"); 

    echo $response->asXML(); 

출력 :

<?xml version="1.0" ?> 
    <actions> 
     <image> 
     <nodexy /> 
     </image> 
    </actions> 
관련 문제