tag replacement in toxml()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manuel Ospina

    #1

    tag replacement in toxml()

    Hi all,

    I am new on the list and I already have a question :-(.

    Ihave something like this:

    import xml.dom.minidom
    from xml.dom.minidom import getDOMImplement ation
    impl = getDOMImplement ation()
    myDoc = impl.createDocu ment(None, "example", None)
    myRoot = myDoc.documentE lement
    myNode1 = myDoc.createEle ment("node")
    myNode2 = myDoc.createEle ment("nodeTwo")
    myText = myDoc.createTex tNode("Here is the <b>problem</>")
    myNode2.appendC hild(myText)
    myNode1.appendC hild(myNode2)
    myRoot.appendCh ild(myNode1)
    print myDoc.toxml()

    The result is:
    '<?xml version="1.0" ?>\n<example><n ode><nodeTwo>He re is the &lt;b&gt;proble m&lt;/&gt;</nodeTwo></node></example>'


    My question is how I can avoid that toxml() replaces the tags?

    Regards,
    Manuel





    _______________ _______________ _______________ _
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.

  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: tag replacement in toxml()

    import xml.dom.minidom
    from xml.dom.minidom import getDOMImplement ation
    impl = getDOMImplement ation()
    myDoc = impl.createDocu ment(None, "example", None)
    myRoot = myDoc.documentE lement
    myNode1 = myDoc.createEle ment("node")
    myNode2 = myDoc.createEle ment("nodeTwo")
    myText = myDoc.createTex tNode("Here is the <b>problem</>")
    myNode2.appendC hild(myText)
    myNode1.appendC hild(myNode2)
    myRoot.appendCh ild(myNode1)
    print myDoc.toxml()
    >
    The result is:
    '<?xml version="1.0" ?>\n<example><n ode><nodeTwo>He re is the &lt;b&gt;proble m&lt;/&gt;</nodeTwo></node></example>'
    >
    >
    My question is how I can avoid that toxml() replaces the tags?
    Gabriel already answered the question: you need to add a 'b'
    element, which has a text child with the text 'problem'; this
    b element needs to be a sibling of the text node 'Here is the '.

    This still won't give you the output "Here is the <b>problem</>",
    as that will insert a closing tag. If you really want to produce
    the text

    '<?xml version="1.0" ?>\n<example><n ode><nodeTwo>He re is the
    <b>problem</></nodeTwo></node></example>'

    you cannot use an XML library to do so: this text is not
    well-formed XML (because </is illegal syntax).

    Regards,
    Martin

    Comment

    Working...