Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19733950
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2010-02-09 14:20:57

rom javax.xml.parsers import DocumentBuilderFactory
from java.io import FileInputStream, ByteArrayInputStream, StringReader
from org.xml.sax import InputSource, EntityResolver

from abstractdomwrapper import AbstractDomWrapper


class DomWrapper(AbstractDomWrapper):
    
    """A wrapper for Java's XML DOM for simplifying reading data from it.
   
    See documentation of AbstractDomWrapper for further usage information.
    """
   
    def __init__(self, path=None, string=None, node=None):
        """Initialize by giving 'path' to an xml file or xml as a 'string'.
       
        Alternative initialization by giving dom 'node' ment to be used only
        internallly.
        """
        AbstractDomWrapper.__init__(self, path)
        if node is None:
            node = self._get_root(path, string)
        self.name = node.tagName
        for item in self._create_list(node.attributes):
            self.attrs[item.name] = item.value
        text = []
        for child in self._create_list(node.childNodes):
            if child.nodeType == child.ELEMENT_NODE:
                self.children.append(DomWrapper(path, node=child))
            elif child.nodeType == child.TEXT_NODE:
                text.append(child.data)
            elif child.nodeType != child.COMMENT_NODE:
                raise TypeError("Unsupported node type: %s" % child.nodeType)
        self.text = ''.join(text)

    def _get_dom(self, path, string):
        if path is not None:
            source = FileInputStream(path)
        else:
            source = ByteArrayInputStream(string)
        factory = DocumentBuilderFactory.newInstance()
        factory.setValidating(False)
        builder = factory.newDocumentBuilder()
        builder.setEntityResolver(_IgnoreDtd())
        return builder.parse(source, 'dummy base uri')
       
    def _create_list(self, items):
        return [ items.item(i) for i in range(items.length) ]

   
class _IgnoreDtd(EntityResolver):
    """EntityResolver that ignores all dtd references from doctypes.
   
    The idea to this hack is based on example at
   
    """
    def resolveEntity(self, publicId, systemId):
        return InputSource(StringReader(''))
 
文件路径:C:\Python26\lib\site-packages\robot\javadomwrapper.py
功能:定义了DomWrapper的基类,XML DOM 定义了访问和处理 XML 文档的标准方法。这里仅仅是java的读,暂不深入 。暂不深入 ,不涉及java。
阅读(25341) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~