0

I need to escape certain NSXMLnodes, treating them as strings? for example i have this (Xliff v1.2 type XML file)

<source>Hello <ph id="1">username</ph></source>

I would like to escape the ph node. Is there an easy way to do this, without pre-scanning the xml and escaping the node beforehand?

gypsyDev
  • 1,232
  • 1
  • 14
  • 22

1 Answers1

0

So Its actually pretty easy to do exactly this with NSXMLParser and XMLDictionary Heres the relevant code changes I made to XMLDictionary.m:

- (void)parser:(__unused NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (![elementNameisEqualToString@"ph"])
    {
         //normal parsing in here
    }
    else
    {
        //manually re-add the element
        NSMutableString *nodeString = [NSMutableString stringWithFormat:@"<"];
        [nodeString appendString:elementName];
        if ([[attributeDict allKeys] count])
        {
            for (NSString *key in attributeDict)
            {
                [nodeString appendString:@" "];
                [nodeString appendString:key];
                [nodeString appendString:@" =\""];
                [nodeString appendString:[attributeDict objectForKey:key]];
                [nodeString appendString:@"\""];
             }
         }
         [nodeString appendString:@">"];
         [self addText:nodeString];
    }

- (void)parser:(__unused NSXMLParser *)parser didEndElement:(__unused NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName
{
    if(![elementNameisEqualToString@"ph"])
    {
        //normal parsing in here
    }
    else
    {
        [self addText:@"</"];
        [self addText:elementName];
        [self addText:@">"];
    }
}
gypsyDev
  • 1,232
  • 1
  • 14
  • 22