Parsing SVG file

I have this svg

<g id="Layer_1">
	<path style="fill:none;stroke:#FA8B00;stroke-width:4;stroke-linecap:round;stroke-miterlimit:10;" d="M1272.8,558l19.2,20l-49,81
		l-18-19L1272.8,558z"/>

how do i access style properties, like color and strokeWeight, parse it in my proper variables and make it something i can use?

my objective is to be able to load an svg file, modify it, add other elements to it than save afterwards.

it neeeded a lot of digging but finally have the solution

void buildShape() {
  println("loading svg");
  original=loadShape(svgFile);
  println("loading XML");
  XML xml=loadXML(svgFile);
  println("parsing XML Children");
  XML[] xmlChildren=xml.getChildren();
  //printArray(xmlChildren);

  //original.disableStyle();
  int numChild=original.getChildCount();
  println("numLayers: "+numChild);


  for (int i=0; i<numChild; i++) {
    PShape tempFace=original.getChild(i);
    XML tempFaceXML=xmlChildren[i+1+i];
    //printArray("tempFaceXML: "+tempFaceXML);
    int numBricks=tempFace.getChildCount();
    println("face: "+i+" numBricks:"+numBricks);


    for (int j=0; j<numBricks; j++) {
      println("face: "+i+" get Brick:"+j);
      PShape temp=tempFace.getChild(j);
      XML tempXML=tempFaceXML.getChild(j+1+j);
      println("tempXML: "+tempXML);
      String style=tempXML.getString("style");
      println("style: "+style);
      String[] styleData=splitTokens(style, "; :");

      println("styleData color: "+styleData[3]);//strokeColor
      println("styleData strokeWeight: "+styleData[5]);//strokeWeight
      color c=unhex("FF"+styleData[3].substring(1));

      pLines.add(new Polyline(c, int(styleData[5])));

      int vCount=temp.getVertexCount();

      for (int k=0; k<vCount; k++) {
        Polyline p=pLines.get(pLines.size()-1);
        PVector pv=temp.getVertex(k);
        p.addVertex(int(pv.x), int(pv.y));
      }
    }
  }
}
1 Like