2010-05-18 5 views
5

나는 "image2.png"와 같은 다른 값으로 "image1.png"를 교체해야하는 노드는 samplexml.svg에서

<image width="744" height="1052" xlink:href="image1.png"/> 

있다. 샘플 코드로 안내해주십시오.

속성 값 "image1.png"를 얻을 수 있습니다. 여기 코드는 다음과 같습니다

여기
$xdoc = new DomDocument; 
$xdoc->Load('samplexml.svg'); 
$tagName = $xdoc->getElementsByTagName('image')->item(0); 
$attribNode = $tagName->getAttributeNode('xlink:href'); 

echo "Attribute Name : " . $attribNode->name . "<br/>"; 
echo "Attribute Value : " . $attribNode->value; 

samplexml.svg된다 HREF 값 :

<svg> 
    <g> 
     <title>Test title</title> 
     <image x="0" y="0" width="744" height="1052" xlink:href="image1.png"/> 
    </g> 
</svg> 

는 어떻게 프로그래밍되는 XLink를 변경합니까?

답변

-2

한 가지 방법은 파일을 문자열로로드 한 다음 검색을 수행하고 바꿀 수 있습니다. 그런 다음 loadXML http://www.php.net/manual/en/domdocument.loadxml.php을 사용하여 변경된 문자열을 매개 변수로 제공 할 수 있습니다.

+3

-1 검색을 제안한는/교체합니다. –

+0

거룩한 암소, 그래서 당신은 더 나은 대안을 제안 할 수 없습니까? 젠체하는 사람을 풀어 라. – zaf

+0

나는 이미 [내 대답] (http://stackoverflow.com/questions/2857113/how-to-change-the-attribute-value-of-svg-file/2857206#2857206)을 남겼습니다. 그냥 downvote에 대한 피드백을 제공. 누군가가 XML/HTML 데이터의 검색/대체를 제안 할 때마다 하나님은 새끼 고양이를 죽입니다. –

13

사용 DOMElement::setAttributeNS() : 그는 이미 DOM을 사용하고 때

$xdoc = new DomDocument; 
$xdoc->Load('svg.xml'); 
$tagName = $xdoc->getElementsByTagName('image')->item(0); 
$attribNode = $tagName->getAttributeNode('xlink:href'); 

echo "Attribute Name : " . $attribNode->name . "<br/>"; 
echo "Attribute Value : " . $attribNode->value; 

$tagName->setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'image2.png'); 

echo $xdoc->saveXML(); 
+0

+1 나를 위해 뭔가 새로운 것을 오늘 배웁니다. – zaf