0

I am using ElementTree. I have a parsed xml document that looks like this:

<data>
    <name>
        <slash>/</slash>
    </name>
</data>

I would like to be able to save it using a hex code for the html escape character.

Since the hex code for '/' is 2F, I would like to persist the xml as:

<data>
    <name>
        <slash>&#x2f;</slash>
    </name>
</data>

What is the pythonic way to do this? Ideally, I would like this to work:

import xml.etree.ElementTree as ET

xml_doc = ET.tostring(source,method="xml")

xml_doc=change_to_html_hex_code(xml_doc)

out_file = open("output.xml",'w')
out_file.write(xml_doc)

2 Answers 2

1

Using lxml:

x = """<data>
    <name>
        <slash>/</slash>
    </name>
</data>"""
import lxml.etree as et

xml_doc = et.fromstring(x)
for node in xml_doc.xpath("//*[contains(text(), '/')]"):
    node.text = node.text.replace("/","&#x2f;")



print(et.tostring(xml_doc))

Which would give you:

<data>
    <name>
        <slash>&amp;#x2f;</slash>
    </name>
</data>

Or xml:

from xml.etree import ElementTree  as et

xml_doc = et.fromstring(x)
for node in xml_doc.iter("*"):
    if "/" in node.text:
        node.text = node.text.replace("/","&#x2f;")

To write to the file, this answer covers both.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the codecs module (included in python)

import codecs

def returnHex(input):
    hexitup = codecs.getencoder('hex')
    return ("&#" + hexitup(input)[0]+";")

print(returnHex('/'))

This should return &#2f;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.