Parse mutilple children attributes of element using cheerio in NodeJs

Viewed 51

I have svg file that looks like this

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="300.000000pt" height="226.000000pt" viewBox="0 0 300.000000 226.000000"
 preserveAspectRatio="xMidYMid meet">

<g transform="translate(0.000000,226.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">

<path d="M891 2162 c-95 -33 -418 -606 -616 -1092 -75 -185 -106 -330 -70 -67 -2 3 7 36 19 75 13 
27 67 2 -3 -7 -36 -19 -75z"/>

<path d="M1316 2019 c-33 -39 -8035 -57 -37 -58 -82 -59 -25 0 -55 -2 -66 -4 -11 -2 -58 -8 -104 
-15 -158 -22 -330 -77 -315 -101 10 -17 85 -5 311 51 -12 -15 -27z"/>

<path d="M925 1349 c-4 -6 -5 -12 -2 -15 2 -3 7 2 10 11 7 17 1 20 -8 4z"/>

</g>
</svg>

I want to get d attribute of each path element and combine it in one svg path string (that consists of all three paths ) I've tried this buy only get first one every time

   fs.readFile(path_to_svg,'utf8',(err,data)=>{
        if(err) console.log(err);
        $ = cheerio.load(data,{ xmlMode : true });
        $('svg').children().each(function(i,path){
            console.log($('path').attr('d'))
        });
    });
1 Answers

You can get those with:

$('svg path').get().map(path => $(path).attr('d'))
Related