from java.io import FileOutputStream
from javax.xml.transform.sax import SAXTransformerFactory
from javax.xml.transform.stream import StreamResult
from org.xml.sax.helpers import AttributesImpl
from abstractxmlwriter import AbstractXmlWriter
class XmlWriter(AbstractXmlWriter):
def __init__(self, path):
self.path = path
self._output = FileOutputStream(path)
self._writer = SAXTransformerFactory.newInstance().newTransformerHandler()
self._writer.setResult(StreamResult(self._output))
self._writer.startDocument()
self.content('\n')
self.closed = False
def start(self, name, attributes={}, newline=True):
attrs = AttributesImpl()
for attrname, attrvalue in attributes.items():
attrs.addAttribute('', '', attrname, '', attrvalue)
self._writer.startElement('', '', name, attrs)
if newline:
self.content('\n')
def content(self, content):
if content is not None:
content = self._encode(content)
self._writer.characters(content, 0, len(content))
def end(self, name, newline=True):
self._writer.endElement('', '', name)
if newline:
self.content('\n')
def close(self):
self._writer.endDocument()
self._output.close()
self.closed = True
文件路径:C:\Python26\lib\site-packages\robot\jyxmlwriter.py
功能:java平台下写入xml的功能,暂不涉及java
阅读(21441) | 评论(0) | 转发(0) |