如何更改SVG文件的属性值 [英] How to change the attribute value of svg file
本文介绍了如何更改SVG文件的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
samplexml.svg中有一个节点
In samplexml.svg there is a node
<image width="744" height="1052" xlink:href="image1.png"/>
我需要将"image1.png"替换为另一个值,例如"image2.png".请用示例代码指导我如何做到这一点.
I need to replace "image1.png" with another value like "image2.png". Please guide me with sample code how to to that.
我可以获得属性值"image1.png".这是代码:
I could get the attribute value "image1.png". Here is the code:
$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:
Here is samplexml.svg:
<svg>
<g>
<title>Test title</title>
<image x="0" y="0" width="744" height="1052" xlink:href="image1.png"/>
</g>
</svg>
如何以编程方式更改xlink:href值?
How do I programmatically change the xlink:href value?
推荐答案
使用 DOMElement :: setAttributeNS():
$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();
这篇关于如何更改SVG文件的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文