Replacing Element Nodes
Simon Dean
simon.dean at Sun.COM
Wed Sep 3 14:29:28 EDT 2003
Hi,
I'm trying to replace Element Node's in 'dom' with Element Node's from
'domFromRef'. The Element Nodes are selected as a result of processing XML ref's
present in 'dom'.
At the moment,
print replacementElementNode
PrettyPrint(replacementElementNode)
prints the Element Nodes which I want to use in my replaceNode function 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 replace the elements ?
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')
reader = PyExpat.Reader()
clone, elementName, domFromRef = getParentElementBlockFromFile(elementName, attFileValue, domFromRef
)
newDom = replaceNode(clone, elementName, domFromRef)
def getParentElementBlockFromFile(elementName, attFileValue, domFromRef):
domFromRef = reader.fromUri('/home/sd/data_3/' +attFileValue +'.xml')
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)
More information about the Python-list
mailing list