[XML-SIG] [Simon.Dean@Sun.COM: Replacing Element Nodes]

Simon Dean simon.dean at sun.com
Sun Sep 7 20:23:33 EDT 2003


Hi,

I'm trying to replace Element Node's in a DOM called 'dom' with Element Node's from 
several DOM's assigned 'domFromRef'. 'domFromRef' get's generated dynamically from list
contents in 'refs'.

print replacementElementNode 
PrettyPrint(replacementElementNode)

prints the Element Nodes which I want to replace in 'dom' e.g. 

<Element Node at 853a04c: Name='LC_COLLATION' with 0 attributes and 5 childern>

<LC_COLLATION>
  <Collator>
  <CollationOptions>
    <...etc>
  <CollationOptions>
</LC_COLLATION>


What should I call now in 'def replaceNode' to do the replacement, Element Nodes in the domFromRef with the
Element Nodes from dom, replaceChild() ?  If so, I can't seem to get it to work.

My program is below, any help much appreciated.

cheers,
Simon.


from    xml.dom.ext.reader      import  PyExpat
from    xml.dom.ext             import  PrettyPrint
from    xml.xpath               import  Evaluate
import  sys

sys.getdefaultencoding()
reader = PyExpat.Reader()
dom = reader.fromStream(sys.stdin)

def reconstructDom(dom):

        refs = Evaluate('/Locale//*[@ref]', dom.documentElement)
        for i in range(len(refs)):
                elementName = refs[i].nodeName

                for parent in dom.getElementsByTagName(elementName):
                        if parent.nodeName == 'LC_FORMAT':
                                attFileValue = parent.getAttribute('ref')
                                attCurrValue = parent.getAttribute('replaceTo')
                                getFormatElementBlockFromFile(elementName, attFileValue, attCurrValue)
                        else: 
                                attFileValue = parent.getAttribute('ref')
				domFromRef = reader.fromUri('/home/sd/data_3/' +attFileValue +'.xml')
                                clone, elementName, domFromRef = getParentElementBlockFromFile(elementName, attFileValue, domFromRef
) 
                                newDom = replaceNode(clone, elementName, domFromRef)



def getParentElementBlockFromFile(elementName, attFileValue, domFromRef):

        for parent in domFromRef.getElementsByTagName(elementName):
                clone = parent.cloneNode(1)
                return clone, elementName, domFromRef 


def getFormatElementBlockFromFile(elementName, attFileValue, attCurrValue):
        pass


def replaceNode(clone, elementName, domFromRef):

        # replace Element Node from domFromRef with Element Node from dom and return dom to higher object 
        replacementElementNode = dom.importNode(clone, 1)
        
	print replacementElementNode 
        PrettyPrint(replacementElementNode)

        #return newDom 


# main()
reconstructDom(dom)

 

-- 
http://mail.python.org/mailman/listinfo/python-list

----- End forwarded message -----

-- 
-------------------------------------------------------------------
simon.dean at Sun.com
Globalization Engineering
http://www.sun.com
These opinions are my own unless otherwise stated.
-------------------------------------------------------------------



 



More information about the XML-SIG mailing list