我们的开发人员拥有这些语言的知识 - Ruby,Python,.Net或Java.我们正在开发一个主要处理XML文档的应用程序.大部分工作是将预定义的XML文件转换为数据库表,通过数据库提供XML文档之间的映射,从数据库创建报告等.哪种语言最简单,最快速?(这是一个网络应用程序)
动态语言规则.为什么?映射很容易编码和更改.您不必重新编译和重建.
实际上,通过一点点聪明,您可以将"XML XPATH转换为标记 - >数据库表字段"映射为主应用程序导入的不相交的Python代码块.
Python代码块是您的配置文件.它不是描述配置的.ini
文件或.properties
文件.这是配置.
我们使用Python,xml.etree和SQLAlchemy(将SQL从程序中分离出来),因为我们只需很少的工作量和灵活性即可运行.
source.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
model.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
loader.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
要进行更改,您可以更正构建器或创建新构建器.您可以调整加载程序源以标识将使用的构建器.您可以毫不费力地选择构建器作为命令行参数.动态语言中的动态导入对我来说似乎有些过分,但它们很方便.
我建议使用XSLT模板根据需要将XML转换为INSERT语句(或任何你需要的).
您应该可以从您提到的任何语言调用XSLT.
与长期合作相比,这将导致更少的代码.
在.NET中,C#3.0和VB9为使用LINQ to XML处理XML提供了出色的支持:
LINQ to XML概述