I am creating a XML document and trying to insert tags in between XMLElement as below
let tEle = XMLElement.element(withName: "xuv") as? XMLElement
let segEle = XMLElement.element(withName: "seg") as? XMLElement
segEle?.stringValue = "Sample Text Here"
tuvEle?.addChild(segEle!)
This is working fine, but for segEle i want to add some tags between text as "Sample Text <ph x="1" type="x-LPH"/> Here", if we notice i need to insert "ph" tag in between the string.
FYI : I tried to insert " <ph x=\"#\" type=\"x-LPH\"/> " this text between string but this didn't worked as in the output xml file the "ph" tag is displayed as <ph x="0" type="x-LPH"/> OBLIGATOIRE <ph x="1" type="x-LPH"/> (if we notice for "<" it is replaced as "<" and for ">" is replaced as">")
Can anyone please let me know how can we handle this.
Try to backwards-engineer how it's supposed to be: inspect the result of
XMLElement(xmlString: "<seg>Sample Text <ph x=\"1\" /> Here")and its children.XMLElementis only suitable for tags;XMLElement.addChildaccepts the more general typeXMLNode.XMLNode.Kindcan be.text, which is what you're looking for instead of the simplerstringValue.So you have to split your simple string value into 3 subnodes:
XMLNode.text(withStringValue: "Sample Text ") as? XMLNodeXMLNode.element(withName: "Sample Text ") as? XMLElementXMLNode.text(withStringValue: " Here") as? XMLNodeThen add all 3 children to the parent note
segEle(which doesn't need astringValueanymore).