本文整理汇总了Python中twisted.web.microdom.parseString函数的典型用法代码示例。如果您正苦于以下问题:Python parseString函数的具体用法?Python parseString怎么用?Python parseString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parseString函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: footnotes
def footnotes(document):
"""
Find footnotes in the given document, move them to the end of the body, and
generate links to them.
A footnote is any node with a C{class} attribute set to C{footnote}.
Footnote links are generated as superscript. Footnotes are collected in a
C{ol} node at the end of the document.
@type document: A DOM Node or Document
@param document: The input document which contains all of the content to be
presented.
@return: C{None}
"""
footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote")
if not footnotes:
return
footnoteElement = microdom.Element("ol")
id = 1
for footnote in footnotes:
href = microdom.parseString('<a href="#footnote-%(id)d">' "<super>%(id)d</super></a>" % vars()).documentElement
text = " ".join(domhelpers.getNodeText(footnote).split())
href.setAttribute("title", text)
target = microdom.Element("a", attributes={"name": "footnote-%d" % id})
target.childNodes = [footnote]
footnoteContent = microdom.Element("li")
footnoteContent.childNodes = [target]
footnoteElement.childNodes.append(footnoteContent)
footnote.parentNode.replaceChild(href, footnote)
id += 1
body = domhelpers.findNodesNamed(document, "body")[0]
header = microdom.parseString("<h2>Footnotes</h2>").documentElement
body.childNodes.append(header)
body.childNodes.append(footnoteElement)
开发者ID:radical-software,项目名称:radicalspam,代码行数:35,代码来源:tree.py
示例2: testNamespaces
def testNamespaces(self):
s = '''
<x xmlns="base">
<y />
<y q="1" x:q="2" y:q="3" />
<y:y xml:space="1">here is some space </y:y>
<y:y />
<x:y />
</x>
'''
d = microdom.parseString(s)
# at least make sure it doesn't traceback
s2 = d.toprettyxml()
self.assertEquals(d.documentElement.namespace,
"base")
self.assertEquals(d.documentElement.getElementsByTagName("y")[0].namespace,
"base")
self.assertEquals(
d.documentElement.getElementsByTagName("y")[1].getAttributeNS('base','q'),
'1')
d2 = microdom.parseString(s2)
self.assertEquals(d2.documentElement.namespace,
"base")
self.assertEquals(d2.documentElement.getElementsByTagName("y")[0].namespace,
"base")
self.assertEquals(
d2.documentElement.getElementsByTagName("y")[1].getAttributeNS('base','q'),
'1')
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:29,代码来源:test_xml.py
示例3: setAuthors
def setAuthors(template, authors):
# First, similarly to setTitle, insert text into an <div class="authors">
text = ''
for name, href in authors:
# FIXME: Do proper quoting/escaping (is it ok to use
# xml.sax.saxutils.{escape,quoteattr}?)
anchor = '<a href="%s">%s</a>' % (href, name)
if (name, href) == authors[-1]:
if len(authors) == 1:
text = anchor
else:
text += 'and ' + anchor
else:
text += anchor + ','
childNodes = microdom.parseString('<span>' + text +'</span>').childNodes
for node in domhelpers.findElementsWithAttribute(template,
"class", 'authors'):
node.childNodes.extend(childNodes)
# Second, add appropriate <link rel="author" ...> tags to the <head>.
head = domhelpers.findNodesNamed(template, 'head')[0]
authors = [microdom.parseString('<link rel="author" href="%s" title="%s"/>'
% (href, name)).childNodes[0]
for name, href in authors]
head.childNodes.extend(authors)
开发者ID:galaxysd,项目名称:BitTorrent,代码行数:27,代码来源:tree.py
示例4: generateToC
def generateToC(document):
"""
Create a table of contents for the given document.
@type document: A DOM Node or Document
@rtype: A DOM Node
@return: a Node containing a table of contents based on the headers of the
given document.
"""
toc, level, id = "\n<ol>\n", 0, 0
for element in getHeaders(document):
elementLevel = int(element.tagName[1]) - 2
toc += (level - elementLevel) * "</ul>\n"
toc += (elementLevel - level) * "<ul>"
toc += '<li><a href="#auto%d">' % id
toc += domhelpers.getNodeText(element)
toc += "</a></li>\n"
level = elementLevel
anchor = microdom.parseString('<a name="auto%d" />' % id).documentElement
element.childNodes.append(anchor)
id += 1
toc += "</ul>\n" * level
toc += "</ol>\n"
return microdom.parseString(toc).documentElement
开发者ID:radical-software,项目名称:radicalspam,代码行数:25,代码来源:tree.py
示例5: testSearch
def testSearch(self):
s = "<foo><bar id='me' /><baz><foo /></baz></foo>"
s2 = "<fOo><bAr id='me' /><bAz><fOO /></bAz></fOo>"
d = microdom.parseString(s)
d2 = microdom.parseString(s2, caseInsensitive=0, preserveCase=1)
d3 = microdom.parseString(s2, caseInsensitive=1, preserveCase=1)
root = d.documentElement
self.assertEquals(root.firstChild(), d.getElementById('me'))
self.assertEquals(d.getElementsByTagName("foo"),
[root, root.lastChild().firstChild()])
root = d2.documentElement
self.assertEquals(root.firstChild(), d2.getElementById('me'))
self.assertEquals(d2.getElementsByTagName('fOo'), [root])
self.assertEquals(d2.getElementsByTagName('fOO'),
[root.lastChild().firstChild()])
self.assertEquals(d2.getElementsByTagName('foo'), [])
root = d3.documentElement
self.assertEquals(root.firstChild(), d3.getElementById('me'))
self.assertEquals(d3.getElementsByTagName('FOO'),
[root, root.lastChild().firstChild()])
self.assertEquals(d3.getElementsByTagName('fOo'),
[root, root.lastChild().firstChild()])
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:25,代码来源:test_xml.py
示例6: testMutate
def testMutate(self):
s = "<foo />"
s1 = '<foo a="b"><bar/><foo/></foo>'
s2 = '<foo a="b">foo</foo>'
d = microdom.parseString(s).documentElement
d1 = microdom.parseString(s1).documentElement
d2 = microdom.parseString(s2).documentElement
d.appendChild(d.cloneNode())
d.setAttribute("a", "b")
child = d.childNodes[0]
self.assertEquals(child.getAttribute("a"), None)
self.assertEquals(child.nodeName, "foo")
d.insertBefore(microdom.Element("bar"), child)
self.assertEquals(d.childNodes[0].nodeName, "bar")
self.assertEquals(d.childNodes[1], child)
for n in d.childNodes:
self.assertEquals(n.parentNode, d)
self.assert_(d.isEqualToNode(d1))
d.removeChild(child)
self.assertEquals(len(d.childNodes), 1)
self.assertEquals(d.childNodes[0].nodeName, "bar")
t = microdom.Text("foo")
d.replaceChild(t, d.firstChild())
self.assertEquals(d.firstChild(), t)
self.assert_(d.isEqualToNode(d2))
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:29,代码来源:test_xml.py
示例7: testOutput
def testOutput(self):
for s, out in self.samples:
d = microdom.parseString(s, caseInsensitive=0)
d2 = microdom.parseString(out, caseInsensitive=0)
testOut = d.documentElement.toxml()
self.assertEquals(out, testOut)
self.assert_(d.isEqualToDocument(d2))
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:7,代码来源:test_xml.py
示例8: testEatingWhitespace
def testEatingWhitespace(self):
s = """<hello>
</hello>"""
d = microdom.parseString(s)
self.failUnless(not d.documentElement.hasChildNodes(),
d.documentElement.childNodes)
self.failUnless(d.isEqualToDocument(microdom.parseString('<hello></hello>')))
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:7,代码来源:test_xml.py
示例9: testLenientAmpersand
def testLenientAmpersand(self):
prefix = "<?xml version='1.0'?>"
# we use <pre> so space will be preserved
for i, o in [("&", "&"), ("& ", "& "), ("&", "&"), ("&hello monkey", "&hello monkey")]:
d = microdom.parseString("%s<pre>%s</pre>" % (prefix, i), beExtremelyLenient=1)
self.assertEqual(d.documentElement.toxml(), "<pre>%s</pre>" % o)
# non-space preserving
d = microdom.parseString("<t>hello & there</t>", beExtremelyLenient=1)
self.assertEqual(d.documentElement.toxml(), "<t>hello & there</t>")
开发者ID:RCBiczok,项目名称:VTK,代码行数:9,代码来源:test_xml.py
示例10: testDoctype
def testDoctype(self):
s = '<?xml version="1.0"?>' '<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">' "<foo></foo>"
s2 = "<foo/>"
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEqual(d.doctype, 'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEqual(d.toxml(), s)
self.failIf(d.isEqualToDocument(d2))
self.failUnless(d.documentElement.isEqualToNode(d2.documentElement))
开发者ID:RCBiczok,项目名称:VTK,代码行数:9,代码来源:test_xml.py
示例11: testSingletons
def testSingletons(self):
s = "<foo><b/><b /><b\n/></foo>"
s2 = "<foo><b/><b/><b/></foo>"
nodes = microdom.parseString(s).documentElement.childNodes
nodes2 = microdom.parseString(s2).documentElement.childNodes
self.assertEquals(len(nodes), 3)
for (n, n2) in zip(nodes, nodes2):
self.assert_(isinstance(n, microdom.Element))
self.assertEquals(n.nodeName, "b")
self.assert_(n.isEqualToNode(n2))
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:10,代码来源:test_xml.py
示例12: test_replaceNonChild
def test_replaceNonChild(self):
"""
L{Node.replaceChild} raises L{ValueError} if the node given to be
replaced is not a child of the node C{replaceChild} is called on.
"""
parent = microdom.parseString("<foo />")
orphan = microdom.parseString("<bar />")
replacement = microdom.parseString("<baz />")
self.assertRaises(ValueError, parent.replaceChild, replacement, orphan)
开发者ID:RCBiczok,项目名称:VTK,代码行数:10,代码来源:test_xml.py
示例13: test_getElementsByTagName
def test_getElementsByTagName(self):
doc1=microdom.parseString('<foo/>')
actual=domhelpers.getElementsByTagName(doc1, 'foo')[0].nodeName
expected='foo'
self.assertEquals(actual, expected)
el1=doc1.documentElement
actual=domhelpers.getElementsByTagName(el1, 'foo')[0].nodeName
self.assertEqual(actual, expected)
doc2_xml='<a><foo in="a"/><b><foo in="b"/></b><c><foo in="c"/></c><foo in="d"/><foo in="ef"/><g><foo in="g"/><h><foo in="h"/></h></g></a>'
doc2=microdom.parseString(doc2_xml)
tag_list=domhelpers.getElementsByTagName(doc2, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
expected='abcdefgh'
self.assertEquals(actual, expected)
el2=doc2.documentElement
tag_list=domhelpers.getElementsByTagName(el2, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
self.assertEqual(actual, expected)
doc3_xml='''
<a><foo in="a"/>
<b><foo in="b"/>
<d><foo in="d"/>
<g><foo in="g"/></g>
<h><foo in="h"/></h>
</d>
<e><foo in="e"/>
<i><foo in="i"/></i>
</e>
</b>
<c><foo in="c"/>
<f><foo in="f"/>
<j><foo in="j"/></j>
</f>
</c>
</a>'''
doc3=microdom.parseString(doc3_xml)
tag_list=domhelpers.getElementsByTagName(doc3, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
expected='abdgheicfj'
self.assertEquals(actual, expected)
el3=doc3.documentElement
tag_list=domhelpers.getElementsByTagName(el3, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
self.assertEqual(actual, expected)
doc4_xml='<foo><bar></bar><baz><foo/></baz></foo>'
doc4=microdom.parseString(doc4_xml)
actual=domhelpers.getElementsByTagName(doc4, 'foo')
root=doc4.documentElement
expected=[root, root.lastChild().firstChild()]
self.assertEquals(actual, expected)
actual=domhelpers.getElementsByTagName(root, 'foo')
self.assertEqual(actual, expected)
开发者ID:AnthonyNystrom,项目名称:YoGoMee,代码行数:55,代码来源:test_domhelpers.py
示例14: testDoctype
def testDoctype(self):
s = ('<?xml version="1.0"?>'
'<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">'
'<foo />')
s2 = '<foo/>'
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEquals(d.doctype, 'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEquals(d.toxml(), s)
self.failIfEqual(d, d2)
self.failUnlessEqual(d.documentElement, d2.documentElement)
开发者ID:fxia22,项目名称:ASM_xf,代码行数:11,代码来源:test_xml.py
示例15: test_doctype
def test_doctype(self):
s = ('<?xml version="1.0"?>'
'<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">'
'<foo></foo>')
s2 = '<foo/>'
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEqual(d.doctype,
'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEqual(d.toxml(), s)
self.assertFalse(d.isEqualToDocument(d2))
self.assertTrue(d.documentElement.isEqualToNode(d2.documentElement))
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:12,代码来源:test_xml.py
示例16: testSpacing
def testSpacing(self):
# testing issue #414
s = "<?xml version='1.0'?><p><q>smart</q> <code>HairDryer</code></p>"
d = microdom.parseString(s, beExtremelyLenient=1)
expected = "<p><q>smart</q> <code>HairDryer</code></p>"
actual = d.documentElement.toxml()
self.assertEquals(expected, actual)
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:7,代码来源:test_xml.py
示例17: checkParsed
def checkParsed(self, input, expected, beExtremelyLenient=1):
"""
Check that C{input}, when parsed, produces a DOM where the XML
of the document element is equal to C{expected}.
"""
output = microdom.parseString(input, beExtremelyLenient=beExtremelyLenient)
self.assertEqual(output.documentElement.toxml(), expected)
开发者ID:RCBiczok,项目名称:VTK,代码行数:7,代码来源:test_xml.py
示例18: test_scrubCIDLinks
def test_scrubCIDLinks(self):
"""
Test L{xquotient.scrubber.Scrubber.scrubCIDLinks} with a bunch of
different nodes
"""
node = parseString("""
<html>
<img src="cid:foo" />
<a href="x" name="1" />
<iframe src="cid:bar" />
<iframe name="2" />
<a href="cid:xxx" />
<img src="123" name="3" />
<link href="cid:foo" />
<link href="xyz" name="4" />
<script src="cid:baz" />
<script href="x" name="5" />
</html>""").documentElement
scrubCIDLinks(node)
self.assertEquals(
list(int(e.attributes['name']) for e in node.childNodes),
[1, 2, 3, 4, 5])
开发者ID:pombredanne,项目名称:quotient,代码行数:25,代码来源:test_scrubber.py
示例19: gotResponse
def gotResponse(response):
log.msg(response)
doc = microdom.parseString(response)
elms = doc.getElementsByTagName("cas:authenticationSuccess")
valid = False
pgt = None
if len(elms) > 0:
valid = True
elms = doc.getElementsByTagName("cas:user")
if len(elms) > 0:
elm = elms[0]
username = elm.childNodes[0].value
elms = doc.getElementsByTagName("cas:proxyGrantingTicket")
if len(elms) > 0:
elm = elms[0]
iou = elm.childNodes[0].value
pgt = None
if iou in self._ious:
pgt = self._ious[iou]
del self._ious[iou]
else:
log.msg("[WARNING] Could not corrolate PGTIOU '%s'." % iou)
if not valid:
raise Exception('Invalid login')
session = request.getSession()
session.username = username
if pgt is not None:
session.pgt = pgt
log.msg("PGT added to session '%s'." % pgt)
request.redirect(request.URLPath().sibling('').path)
开发者ID:cwaldbieser,项目名称:txcas,代码行数:30,代码来源:sample.py
示例20: testAwfulTagSoup
def testAwfulTagSoup(self):
s = """
<html>
<head><title> I send you this message to have your advice!!!!</titl e
</headd>
<body bgcolor alink hlink vlink>
<h1><BLINK>SALE</blINK> TWENTY MILLION EMAILS & FUR COAT NOW
FREE WITH `ENLARGER'</h1>
YES THIS WONDERFUL AWFER IS NOW HERER!!!
<script LANGUAGE="javascript">
function give_answers() {
if (score < 70) {
alert("I hate you");
}}
</script><a href=/foo.com/lalal name=foo>lalal</a>
</body>
</HTML>
"""
d = microdom.parseString(s, beExtremelyLenient=1)
l = domhelpers.findNodesNamed(d.documentElement, 'blink')
self.assertEquals(len(l), 1)
开发者ID:andrewbird,项目名称:vodafone-mobile-connect,代码行数:25,代码来源:test_xml.py
注:本文中的twisted.web.microdom.parseString函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论