python哪种语言最好_哪种语言最容易和最快地处理XML内容?

一种动态的语言规则。为什么?映射易于编码和更改。你不必重新编译和重建。

实际上,只要稍微聪明一点,您就可以将“XML XPATH to a Tag->DB table field”映射为主应用程序导入的不相交的Python代码块。

Python代码块是配置文件。描述配置的不是.ini文件或.properties文件。它是配置。

我们使用Python、xml.etree和SQLAlchemy(将SQL从程序中分离出来)来实现这一点,因为我们在启动和运行时只需很少的努力和很大的灵活性。

源.py"""A particular XML parser. Formats change, so sometimes this changes, too."""

import xml.etree.ElementTree as xml

class SSXML_Source( object ):

ns0= "urn:schemas-microsoft-com:office:spreadsheet"

ns1= "urn:schemas-microsoft-com:office:excel"

def __init__( self, aFileName, *sheets ):

"""Initialize a XML source.

XXX - Create better sheet filtering here, in the constructor.

@param aFileName: the file name.

"""

super( SSXML_Source, self ).__init__( aFileName )

self.log= logging.getLogger( "source.PCIX_XLS" )

self.dom= etree.parse( aFileName ).getroot()

def sheets( self ):

for wb in self.dom.getiterator("{%s}Workbook" % ( self.ns0, ) ):

for ws in wb.getiterator( "{%s}Worksheet" % ( self.ns0, ) ):

yield ws

def rows( self ):

for s in self.sheets():

print s.attrib["{%s}Name" % ( self.ns0, ) ]

for t in s.getiterator( "{%s}Table" % ( self.ns0, ) ):

for r in t.getiterator( "{%s}Row" % ( self.ns0, ) ):

# The XML may not be really useful.

# In some cases, you may have to convert to something useful

yield r

模型.py"""This is your target object.

It's part of the problem domain; it rarely changes.

"""

class MyTargetObject( object ):

def __init__( self ):

self.someAttr= ""

self.anotherAttr= ""

self.this= 0

self.that= 3.14159

def aMethod( self ):

"""etc."""

pass

builder_today.py许多映射配置之一"""One of many builders. This changes all the time to fit

specific needs and situations. The goal is to keep this

short and to-the-point so that it has the mapping and nothing

but the mapping.

"""

import model

class MyTargetBuilder( object ):

def makeFromXML( self, element ):

result= model.MyTargetObject()

result.someAttr= element.findtext( "Some" )

result.anotherAttr= element.findtext( "Another" )

result.this= int( element.findtext( "This" ) )

result.that= float( element.findtext( "that" ) )

return result

加载程序.py"""An application that maps from XML to the domain object

using a configurable "builder".

"""

import model

import source

import builder_1

import builder_2

import builder_today

# Configure this: pick a builder is appropriate for the data:

b= builder_today.MyTargetBuilder()

s= source.SSXML_Source( sys.argv[1] )

for r in s.rows():

data= b.makeFromXML( r )

# ... persist data with a DB save or file write

要进行更改,可以更正生成器或创建新生成器。调整加载程序源以标识将使用哪个生成器。您可以将生成器的选择设置为命令行参数,而无需太多麻烦。动态语言中的动态导入对我来说似乎有点过头了,但它们很方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值