def _create_fragment_test_nodes():
    dom = parseString("<doc/>")
    orig = dom.createTextNode("original")
    c1 = dom.createTextNode("foo")
    c2 = dom.createTextNode("bar")
    c3 = dom.createTextNode("bat")
    dom.documentElement.appendChild(orig)
    frag = dom.createDocumentFragment()
    frag.appendChild(c1)
    frag.appendChild(c2)
    frag.appendChild(c3)
    return dom, orig, c1, c2, c3, frag
예제 #2
0
def _create_fragment_test_nodes():
    dom = parseString("<doc/>")
    orig = dom.createTextNode("original")
    c1 = dom.createTextNode("foo")
    c2 = dom.createTextNode("bar")
    c3 = dom.createTextNode("bat")
    dom.documentElement.appendChild(orig)
    frag = dom.createDocumentFragment()
    frag.appendChild(c1)
    frag.appendChild(c2)
    frag.appendChild(c3)
    return dom, orig, c1, c2, c3, frag
예제 #3
0
def testLegalChildren():
    dom = Document()
    elem = dom.createElement('element')
    text = dom.createTextNode('text')

    try: dom.appendChild(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    dom.appendChild(elem)
    try: dom.insertBefore(text, elem)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    try: dom.replaceChild(text, elem)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    nodemap = elem.attributes
    try: nodemap.setNamedItem(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"

    try: nodemap.setNamedItemNS(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr"

    elem.appendChild(text)
    dom.unlink()
def testLegalChildren():
    dom = Document()
    elem = dom.createElement('element')
    text = dom.createTextNode('text')

    try: dom.appendChild(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    dom.appendChild(elem)
    try: dom.insertBefore(text, elem)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    try: dom.replaceChild(text, elem)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "dom.appendChild didn't raise HierarchyRequestErr"

    nodemap = elem.attributes
    try: nodemap.setNamedItem(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"

    try: nodemap.setNamedItemNS(text)
    except xml.dom.HierarchyRequestErr: pass
    else:
        print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr"

    elem.appendChild(text)
    dom.unlink()
예제 #5
0
def Indent(dom, node, indent=0):
    # Copy child list because it will change soon
    children = node.childNodes[:]
    # Main node doesn't need to be indented

    if indent:
        text = dom.createTextNode('\n' + '\t' * indent)
        node.parentNode.insertBefore(text, node)
    if children:
        # Append newline after last child, except for text nodes
        if children[-1].nodeType == node.ELEMENT_NODE:
            text = dom.createTextNode('\n' + '\t' * indent)
            node.appendChild(text)
            # Indent children which are elements
            for n in children:
                if n.nodeType == node.ELEMENT_NODE:
                    Indent(dom, n, indent + 1)
예제 #6
0
def set_text(node, text):
    '''Set the text of a parent'''
    node.normalize()
    child = node.firstChild
    for child in iter_children(node, xml.dom.Node.TEXT_NODE):
        child.data = text
        return
    dom = get_dom(node)
    node.appendChild(dom.createTextNode(text))
예제 #7
0
        def add_text_node(element, words, padding=False):
            if not words:
                return

            if padding:
                words += ' '

            text = ' '.join(words)
            t = dom.createTextNode(text)
            element.appendChild(t)
예제 #8
0
def makeEasyTag(dom, tagname, value, type='text'):
    tag = dom.createElement(tagname)
    if value.find(']]>') > -1:
        type = 'text'
    if type == 'text':
        value = value.replace('&', '&amp;')
        value = value.replace('<', '&lt;')
        text = dom.createTextNode(value)
    elif type == 'cdata':
        text = dom.createCDATASection(value)
    tag.appendChild(text)
    return tag
예제 #9
0
def generate_xml_from_dict(input_dict, xml_file):

    impl = xml.dom.getDOMImplementation()
    dom = impl.createDocument(None, 'root', None)
    root = dom.documentElement
    instance = dom.createElement('instance')
    root.appendChild(instance)

    for (key, value) in sorted(input_dict.items()):
        nameE = dom.createElement(key)
        nameT = dom.createTextNode(str(value))
        nameE.appendChild(nameT)
        instance.appendChild(nameE)

    with open(xml_file, 'w', encoding='utf-8') as tp:
        dom.writexml(tp, addindent='    ', newl='\n', encoding='utf-8')
예제 #10
0
            versionNodes = fzp.getElementsByTagName("version")
            version = None
            if (versionNodes.length == 0):
                version = dom.createElement("version")
                fzp.insertBefore(version, fzp.firstChild)
            elif (versionNodes.length == 1):
                txt = ""
                version = versionNodes.item(0)
            else:
                print "multiple version elements in", fzpFilename
                continue

            while version.hasChildNodes():
                oldChild = version.firstChild
                version.removeChild(oldChild)
                oldChild.unlink()

            vtext = dom.createTextNode("4")
            version.appendChild(vtext)

            print "adding version 4 to", fzpFilename
            outfile = open(fzpFilename, 'wb')
            s = dom.toxml("UTF-8")
            outfile.write(s)
            outfile.flush()
            outfile.close()


if __name__ == "__main__":
    main()
예제 #11
0
            tagNodes = tags.getElementsByTagName("tag")
            smd = None
            for tag in tagNodes:
                tag.normalize()  #make sure all the text is in one node
                t = None
                for child in tag.childNodes:
                    if child.nodeType == child.TEXT_NODE:
                        t = child
                        break
                if t:
                    if t.nodeValue.lower() == "smd":
                        smd = t
                        break

            if smd == None:
                smd = dom.createElement("tag")
                tags.appendChild(smd)
                t = dom.createTextNode("SMD")
                smd.appendChild(t)

                print "adding smd tag to", fzpFilename
                outfile = open(fzpFilename, 'wb')
                s = dom.toxml("UTF-8")
                outfile.write(s)
                outfile.flush()
                outfile.close()


if __name__ == "__main__":
    main()
예제 #12
0
            propertyNodes = properties.getElementsByTagName("property")
            package = None
            for property in propertyNodes:
                name = property.getAttribute("name")
                #print "got name", name
                if name == "package":
                    package = property
                    break
 
            #print "got package", package
 
            if package == None:
                package = dom.createElement("property")
                properties.appendChild(package)
                package.setAttribute("name", "package")
                t = dom.createTextNode(typ)
                package.appendChild(t)
            else:
                package.normalize()  #make sure all the text is in one node
                t = None
                for child in package.childNodes:
                    if child.nodeType == child.TEXT_NODE:
                        t = child
                        break                        
                
                if t == None:
                    t = dom.createTextNode(typ)
                    package.appendChild(t)
                else:
                    if ("SMD" in t.nodeValue):
                        continue
예제 #13
0
            tagNodes = tags.getElementsByTagName("tag")
            smd = None
            for tag in tagNodes:
                tag.normalize()  # make sure all the text is in one node
                t = None
                for child in tag.childNodes:
                    if child.nodeType == child.TEXT_NODE:
                        t = child
                        break
                if t:
                    if t.nodeValue.lower() == "smd":
                        smd = t
                        break

            if smd == None:
                smd = dom.createElement("tag")
                tags.appendChild(smd)
                t = dom.createTextNode("SMD")
                smd.appendChild(t)

                print "adding smd tag to", fzpFilename
                outfile = open(fzpFilename, "wb")
                s = dom.toxml("UTF-8")
                outfile.write(s)
                outfile.flush()
                outfile.close()


if __name__ == "__main__":
    main()
예제 #14
0
            if (versionNodes.length == 0):
                version = dom.createElement("version")
                fzp.insertBefore(version, fzp.firstChild)
            elif (versionNodes.length == 1):
                txt = ""
                version = versionNodes.item(0)                
            else:
                print "multiple version elements in", fzpFilename
                continue
                
            while version.hasChildNodes():
                oldChild = version.firstChild
                version.removeChild(oldChild)
                oldChild.unlink()
                
            vtext = dom.createTextNode("4")
            version.appendChild(vtext)
            
            print "adding version 4 to", fzpFilename
            outfile = open(fzpFilename, 'wb')
            s = dom.toxml("UTF-8")
            outfile.write(s)
            outfile.flush()
            outfile.close()                    
            
if __name__ == "__main__":
        main()



예제 #15
0
for thesis in theses:
		#if unititle year equals thesis year:
	for sub in subseries:
		if sub.getElementsByTagName("unittitle")[0].firstChild.nodeValue == thesis["unitdate"]:
			#create c with refid and level <c id="{refid}" level="item">
			c = dom.createElement("c")
			c.attributes["id"] = thesis["refid"]
			c.attributes["level"] = "item"
			sub.appendChild(c)	
			#create did
			did = dom.createElement("did") 
			c.appendChild(did)
			#create unittitle tag
			unittitle = dom.createElement("unittitle")
			did.appendChild(unittitle)
			unittitle.appendChild(dom.createTextNode(thesis["unittitle"]))
			#create langmaterial tag 	
			langmaterial = dom.createElement("langmaterial")
			did.appendChild(langmaterial)
			language = dom.createElement("language")
			language.attributes["langcode"] = "eng"
			langmaterial.appendChild(language)
			#create container tag
			container = dom.createElement("container")
			container.attributes["type"] = thesis["container_type"]
			container.attributes["label"] = thesis["container_label"]
			did.appendChild(container)
			container.appendChild(dom.createTextNode(""))
			#create phydesc tag
			physdesc = dom.createElement("physdesc")
			did.appendChild(physdesc)
예제 #16
0
domTagList = domTAG.getElementsByTagName('tag')
domEpisodeList = dom.getElementsByTagName('number')

property = 'west germany'
tagName = 'country'

propertyTags = [
    tag for tag in domTagList if tag.firstChild.nodeValue == property
]
parentOfTags = [tag.parentNode for tag in propertyTags]

qualMovies = [movie.firstChild.firstChild.nodeValue for movie in parentOfTags]

for movie in qualMovies:
    colorEpisode = [
        episode.parentNode for episode in domEpisodeList
        if episode.firstChild.nodeValue == movie
    ]

    episodeNode = colorEpisode[0]

    newElement = dom.createElement(tagName)
    newText = dom.createTextNode(property)
    newElement.appendChild(newText)

    genreElements = episodeNode.getElementsByTagName('genre')
    episodeNode.insertBefore(newElement, genreElements[0])

with open('episodelist-out.xml', 'w', encoding='utf-8') as f:
    f.write(dom.toxml('utf-8').decode('utf-8'))