renggli / dart-xml

Lightweight library for parsing, traversing, and transforming XML in Dart.
http://pub.dartlang.org/packages/xml
MIT License
223 stars 52 forks source link

Modify XmlEments of XmlDocument #133

Closed alloza95 closed 2 years ago

alloza95 commented 2 years ago

I have this SVG:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 25.2.3, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 1000 969.9" enable-background="new 0 0 1000 969.9" xml:space="preserve">

<g id="Legend">
    <path id="TF-9" fill="#54D354" d="M493.3,375.8c0,5-3.9,8.8-8.8,8.8s-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C489.4,367.6,493.3,371.4,493.3,375.8z"/>
    <path id="TF-8" fill="#54D354" d="M547.3,375.8c0,5-3.9,8.8-8.8,8.8c-5,0-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C543.4,367.6,547.3,371.4,547.3,375.8z"/>
    <path id="TF-7" fill="#54D354" d="M556.1,492.4c0,5-3.9,8.8-8.8,8.8c-5,0-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C552.3,484.1,556.1,488,556.1,492.4z"/>
    <path id="TF-6" fill="#54D354" d="M486,492.4c0,5-3.9,8.8-8.8,8.8s-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C482.1,484.1,486,488,486,492.4z"/>
    <path id="TF-5" fill="#54D354" d="M487.2,557.6c0,5-3.9,8.8-8.8,8.8c-5,0-8.8-3.9-8.8-8.8
        s3.9-8.8,8.8-8.8C483.4,549.3,487.2,553.2,487.2,557.6z"/>
    <path id="TF-4" fill="#54D354" d="M556.1,557.6c0,5-3.9,8.8-8.8,8.8c-5,0-8.8-3.9-8.8-8.8
        s3.9-8.8,8.8-8.8C552.3,549.3,556.1,553.2,556.1,557.6z"/>
    <path id="TF-3" fill="#54D354" d="M664.5,588.8c0,5-3.9,8.8-8.8,8.8s-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C660.6,580.6,664.5,584.4,664.5,588.8z"/>
    <path id="TF-2" fill="#54D354" d="M664.5,492.4c0,5-3.9,8.8-8.8,8.8s-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C660.6,484.1,664.5,488,664.5,492.4z"/>
    <path id="TF-1" fill="#54D354" d="M669.3,382.7c0,5-3.9,8.8-8.8,8.8s-8.8-3.9-8.8-8.8
        c0-5,3.9-8.8,8.8-8.8C665.4,374.4,669.3,378.3,669.3,382.7z"/>
</g>
</svg>

When I have created the XmlDocument from SVG string, is it possible modify the attribute "fill" of all "path" XmlElement?

renggli commented 2 years ago

Sure, what about something along:

final document = XmlDocument.parse(svgXml);
document.findAllElements('path')
    .forEach((element) => element.setAttribute('fill', '#000'));
print(document.toXmlString());
alloza95 commented 2 years ago

Thank you very much! This is exactly what I need.