0

I'm parsing an SVG document, to extract the paths that it contains and convert them into UIBezier paths. However, in some files, paths are in groups that have transforms. The destination for the data doesn't have a hierarchical structure, so I have to extract the transforms from the containing groups for any path, and apply it to each path using an AffineTransform.

I can successfully detect the transform in the containing paths and grab the data required. Because the source files are potentially hierarchical, there could be transforms within transforms, so I need to store them in an array.

How do I detect the end of an element, so I can then pop the transform off the array? The parser doesn't seem to respond to the "</g>" tag that closes the element

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Dan Donaldson
  • 1,061
  • 1
  • 8
  • 21
  • I've added a [Community Wiki](https://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts) answer for you. If you want to add your own answer instead, I'm happy to delete that. – Robert Longson Oct 31 '21 at 09:34

1 Answers1

0

To expand on this solution I created a second parse() function that passes the didEndElement where the didStartElement is, and without the attributesDict passed (since it's the last element and has no attributes).

This gets triggered in the conditions required; and from there I can build the code I want.

here's a sample:

func parser(_ parser: XMLParser,
                didEndElement elementName: String,
                namespaceURI: String?,
                qualifiedName qName: String?) {
        if elementName == "g" {
            transforms.removeLast()
        }
    }

Answer copied from Dan Donaldson's edit to this question.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242