Replace value of node using getElementsByTagName

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ouray Viney

    Replace value of node using getElementsByTagName

    Xml

    <ib>8.4.27.5</ib>

    python

    from xml.dom import minidom
    xmldoc = minidom.parse(' C:\TestProfile. xml')
    xmldoc

    ibNodeList = xmldoc.getEleme ntsByTagName("i b")
    firstChild = xmldoc.firstChi ld

    for node in xmldoc.getEleme ntsByTagName('i b'): # visit every node
    <ib>
    print node.toxml()
    node.replaceChi ld("<ib>8.4.27. 5</ib>",node)

    Error

    Traceback (most recent call last):
    File "C:\Python25\Li b\site-packages\python win\pywin\frame work
    \scriptutils.py ", line 310, in RunScript
    exec codeObject in __main__.__dict __
    File "C:\Documen ts and Settings\vineyo \Desktop\parseX ml.py", line
    17, in <module>
    node.firstChild .replaceChild(" <ib>8.4.27.5</ib>",node.first Child)
    File "C:\Python25\li b\xml\dom\minid om.py", line 899, in replaceChild
    self.nodeName + " nodes do not have children")
    HierarchyReques tErr: #text nodes do not have children

    Question:
    Is there an easy way to replace the node value of <ib>? Perhaps I am
    trying to use the wrong python XML library? Any help would be greatly
    appreciated.
  • Ken Starks

    #2
    Re: Replace value of node using getElementsByTa gName

    Ouray Viney wrote:
    Xml
    >
    <ib>8.4.27.5</ib>
    >
    python
    >
    from xml.dom import minidom
    xmldoc = minidom.parse(' C:\TestProfile. xml')
    xmldoc
    >
    ibNodeList = xmldoc.getEleme ntsByTagName("i b")
    firstChild = xmldoc.firstChi ld
    >
    for node in xmldoc.getEleme ntsByTagName('i b'): # visit every node
    <ib>
    print node.toxml()
    node.replaceChi ld("<ib>8.4.27. 5</ib>",node)
    >
    Error
    >
    Traceback (most recent call last):
    File "C:\Python25\Li b\site-packages\python win\pywin\frame work
    \scriptutils.py ", line 310, in RunScript
    exec codeObject in __main__.__dict __
    File "C:\Documen ts and Settings\vineyo \Desktop\parseX ml.py", line
    17, in <module>
    node.firstChild .replaceChild(" <ib>8.4.27.5</ib>",node.first Child)
    File "C:\Python25\li b\xml\dom\minid om.py", line 899, in replaceChild
    self.nodeName + " nodes do not have children")
    HierarchyReques tErr: #text nodes do not have children
    >
    Question:
    Is there an easy way to replace the node value of <ib>? Perhaps I am
    trying to use the wrong python XML library? Any help would be greatly
    appreciated.
    I use 4suite myself.

    see section 3.2.1 of the manual:

    """
    3.2.1 What about getElementsByTa gName()?

    The getElementsByTa gName() method isn't supported, because there are
    better options. In particular, you can just use XPath:

    doc.xpath(u"//tagname")

    For more possibilities, see getElementsByTa gName Alternatives.


    """

    Comment

    • Stefan Behnel

      #3
      Re: Replace value of node using getElementsByTa gName

      Ouray Viney wrote:
      Is there an easy way to replace the node value of <ib>? Perhaps I am
      trying to use the wrong python XML library?
      Looks like it. Try ElementTree.

      from xml.etree import ElementTree
      tree = ElementTree.par se("yourfile.xm l")
      for ib in tree.findall("//ib"):
      ib.text = calculate_new_v alue(ib.text)

      Stefan

      Comment

      • Ouray Viney

        #4
        Re: Replace value of node using getElementsByTa gName

        On Aug 10, 9:01 am, Stefan Behnel <stefan...@behn el.dewrote:
        Ouray Viney wrote:
        Is there an easy way to replace the node value of <ib>?  Perhaps I am
        trying to use the wrong python XML library?
        >
        Looks like it. Try ElementTree.
        >
            from xml.etree import ElementTree
            tree = ElementTree.par se("yourfile.xm l")
            for ib in tree.findall("//ib"):
                ib.text = calculate_new_v alue(ib.text)
        >
        Stefan
        Hi:

        Thank you all for your responses. I was looking at ElementTree
        (http://effbot.org/zone/pythondoc-elementtree-
        ElementTree.htm #elementtree.El ementTree.Eleme ntTree-class). It is
        still unclear to me how I could change the value in the <ibelement.
        I reviewed the availed methods and didn't find anything suitable.

        In your example you show:

        ib.text = calculate_new_v alue(ib.text)

        I don't know what calculate_new_v alue() represents.

        What I am looking for is the ability to do something like this:

        from xml.etree import ElementTree
        tree = ElementTree.par se("C:\test.xml ")
        for ib in tree.findall("//ib"):
        ib.text = "somethingn ew"

        I am also guessing I will need to write the new changes to the file.

        tree.write("C:\ text.xml")

        Good news, while writing this question I tested out the code and it
        worked :).

        Please excuse my trivial questions, I am new to python and appreciate
        your help.

        Kind Rgds

        Comment

        • Stefan Behnel

          #5
          Re: Replace value of node using getElementsByTa gName

          Ouray Viney wrote:
          In your example you show:
          >
          ib.text = calculate_new_v alue(ib.text)
          >
          I don't know what calculate_new_v alue() represents.
          It's meant as pseudo-code. Just take the function name literally and replace
          it by something that gives you the value you want to assign (as you already
          did in your code).

          Good news, while writing this question I tested out the code and it
          worked :).
          Happy to see you've found it. Writing down a question is fairly often enough
          to actually solve it.

          Please excuse my trivial questions, I am new to python and appreciate
          your help.
          You're welcome.

          Stefan

          Comment

          Working...