def test_strip_attr(self): xml = """ <demo depth="1" show="demo"> <foo depth="2" show="foo"> <subfoo depth="3" show="subfoo"> subfoo </subfoo> </foo> <bar depth="2" show="bar-1">bar-1</bar> <bar depth="2" show="bar-2">bar-2</bar> </demo> """ a = { 'demo': defaultdict( dict, { 'attrs': { 'depth': '1', 'show': 'demo' }, 'values': { 'bar': [{ 'attrs': { 'depth': '2', 'show': 'bar-1' }, 'values': 'bar-1' }, { 'attrs': { 'depth': '2', 'show': 'bar-2' }, 'values': 'bar-2' }], 'foo': defaultdict( dict, { 'attrs': { 'depth': '2', 'show': 'foo' }, 'values': { 'subfoo': { 'attrs': { 'depth': '3', 'show': 'subfoo' }, 'values': 'subfoo' } } }) } }) } b = {'demo': {'bar': ['bar-1', 'bar-2'], 'foo': {'subfoo': 'subfoo'}}} self.assertEqual( lazyxml.loads(xml, strip_root=False, strip_attr=False), a) self.assertEqual(lazyxml.loads(xml, strip_root=False, strip_attr=True), b)
def test_namespace(self): xml = """ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="artist"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> """ a = { 'template': { 'html': { 'body': { 'h2': 'My CD Collection', 'table': { 'for-each': { 'tr': { 'td': [{ 'value-of': '' }, { 'value-of': '' }] } }, 'tr': { 'th': ['Title', 'Artist'] } } } } } } self.assertDictEqual(lazyxml.loads(xml), a)
def main(): xml = '<demo><foo>foo</foo><bar>1</bar><bar>2</bar></demo>' # 默认 print lazyxml.loads(xml) # 去除根节点 print lazyxml.loads(xml, strip_root=False) xml = ''' <demo depth="1" show="demo"> <foo depth="2" show="foo"> <subfoo depth="3" show="subfoo"> subfoo </subfoo> </foo> <bar depth="2" show="bar-1">bar-1</bar> <bar depth="2" show="bar-2">bar-2</bar> </demo> ''' print lazyxml.loads(xml, strip_root=False, strip_attr=False) # html实体字符转换 xml = '<root xmlns:h="http://www.w3.org/TR/html4/"><demo><foo>foo</foo><bar>bar</bar></demo></root>' print lazyxml.loads(xml, unescape=True) # 自动猜测编码 也可以手动执行编码 with open('xml/gbk.xml', 'rb') as fp: xml = fp.read() print lazyxml.loads(xml) # 从文件句柄加载xml 命名空间会自动过滤 with open('xml/namespace.xml', 'rb') as fp: print lazyxml.load(fp) # 从类文件对象加载xml from cStringIO import StringIO buffer = StringIO('<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>') print lazyxml.load(buffer) buffer.close()
def _load(self): xml = open(self._getTestcasePath()).read() dicts = lazyxml.loads(xml, strip=False) if not dicts or not dicts['testcases'] or not dicts['testcases']['testcase']: raise Exception("no testcase found") package = dicts['package'] if type(dicts['testcases']['testcase']) is dict: self.testcaseList.append(self._readTestcase(dicts['testcases']['testcase'], package)) else: for testcaseDict in dicts['testcases']['testcase']: self.testcaseList.append(self._readTestcase(testcaseDict, package))
def test_encoding(self): xml = u""" <?xml version="1.0" encoding="gbk"?> <string xmlns="http://www.w3.org/TR/html4/"> <Response> <Result>true</Result> <Reason>保存成功</Reason> </Response> </string> """.encode('gbk') a = { 'Response': { 'Reason': u'\u4fdd\u5b58\u6210\u529f', 'Result': 'true' } } self.assertDictEqual(lazyxml.loads(xml), a)
def _load(self): xml = open(self._getTestcasePath()).read() dicts = lazyxml.loads(xml, strip=False) if not dicts or not dicts['testcases'] or not dicts['testcases']['testcase']: raise Exception("no testcase found") package = dicts['package'] self.version = dicts['version'].replace('.', '') self.init = self.splitCommandLine(dicts['init']) self.setup = self.splitCommandLine(dicts['setup']) if 'setup' in dicts else [] if type(dicts['testcases']['testcase']) is dict: self.testcaseList.append(self._readTestcase(dicts['testcases']['testcase'], package)) else: for testcaseDict in dicts['testcases']['testcase']: self.testcaseList.append(self._readTestcase(testcaseDict, package)) TestcaseNumDao().insert(self.uuid, len(self.testcaseList))
def test_unescape(self): xml = '<root xmlns:h="http://www.w3.org/TR/html4/"><demo><foo>foo</foo><bar>bar</bar></demo></root>' a = {'demo': {'bar': 'bar', 'foo': 'foo'}} b = {} self.assertDictEqual(lazyxml.loads(xml, unescape=True), a) self.assertDictEqual(lazyxml.loads(xml, unescape=False), b)
def test_strip_root(self): xml = '<demo><foo>foo</foo><bar>1</bar><bar>2</bar></demo>' a = {'bar': ['1', '2'], 'foo': 'foo'} b = {'demo': {'bar': ['1', '2'], 'foo': 'foo'}} self.assertDictEqual(lazyxml.loads(xml), a) self.assertDictEqual(lazyxml.loads(xml, strip_root=False), b)
def __init__(self): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) xml = open(os.path.join(__location__, 'config.xml')).read() self.dicts = lazyxml.loads(xml, strip=False)
def __init__(self): __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) xml = open(os.path.join(__location__, 'config.xml')).read() self.dicts = lazyxml.loads(xml, strip=False)