1

I am trying to insert xml nodes in this document:

</providers>

</root>

I wrote this code: import xml.dom.minidom as m

doc = m.parse("monfichier.xml")
valeurs = doc.getElementsByTagName("providers")
element = doc.createElement("provider")
valeurs.appendChild(element)

elthost = doc.createElement("hostnamep") 
eltLTVC = doc.createElement("LocalTrustValueC")
element.appendchild(elthost)
element.appendchild(eltLTVC)

texteHost = doc.createTextNode("machinename")
texteLTVC = doc.createTextNode("23") 
eltHost.appendChild(texteHost)
eltLTVC.appendChild(texteLTVC)
doc.writexml(open("monfichier.xml","w"))

And I want to obtain at the end this xml document : machinename 23

    </provider> 
</providers>

</root>

But I obtained this error : valeurs.appendChild(element) AttributeError: 'NodeList' object has no attribute 'appendChild'

9
  • Such operations are much easier to express with XSLT. Would you be interested in an XSLT solution? Commented Jun 17, 2012 at 19:11
  • Thank you, but I am interested in XML using xml.dom.minidom Any help is appreciated Commented Jun 17, 2012 at 19:23
  • Be aware that it is waste of time not to use XSLT for any XML transformation. Commented Jun 17, 2012 at 19:42
  • Thanks for the advice. So what would be the XSLT solution? Commented Jun 17, 2012 at 19:57
  • I just posted a complete XSLT solution. Note that the first template is standard and nobody has to think to invent it -- it is even added by default by some XSLT IDE's. So you just match the element to which you want to add children, and specify these children literally in-line. Commented Jun 17, 2012 at 20:23

2 Answers 2

2

As per the OP's interest in an XSLT solution:

Here is a complete and short XSLT solution:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="providers">
  <providers>
        <provider>
            <hostnamep>machinename</hostnamep>
            <LocalTrustValueC>23</LocalTrustValueC>
        </provider>
  </providers>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<root>
    <providers> 
    </providers>
</root>

the wanted, correct result is produced:

<root>
   <providers>
      <provider>
         <hostnamep>machinename</hostnamep>
         <LocalTrustValueC>23</LocalTrustValueC>
      </provider>
   </providers>
</root>
Sign up to request clarification or add additional context in comments.

Comments

1

Based on some quick reading of http://docs.python.org/library/xml.dom.html#dom-node-objects it appears that NodeList does not have an appendChild method. Instead you want to get the first Node in the result set(since your post implies there is only one) and call appendChild on that node.

valeurs = doc.getElementsByTagName("providers").item(0)
element = doc.createElement("provider")
valeurs.appendChild(element)

3 Comments

After testing you proposition I get another error related to "element": element.appendchild(elthost) AttributeError: Element instance has no attribute 'appendchild'
Can't remember, is python case sensitive? your comment refers to 'appendchild' without a lower case 'c' in 'child'. Did you try the code with a capital C?
Really thank you very much Flynn. This was just my error.A careless mistake!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.