我们的开发人员了解这些语言 - Ruby、Python、.Net 或 Java。我们正在开发一个主要处理 XML 文档的应用程序。大部分工作是将预定义的 XML 文件转换为数据库表,通过数据库提供 XML 文档之间的映射,从数据库创建报告等。哪种语言最容易和最快使用? (这是一个网络应用程序)
最佳答案
为此的动态语言规则。为什么?映射易于编码和更改。您不必重新编译和重建。
确实,只要稍微聪明一点,您就可以将“XML XPATH to a Tag -> DB table-field”映射作为主应用程序导入的不相交的 Python 代码块。
Python 代码块是您的配置文件。它不是描述配置的 .ini
文件或 .properties
文件。它是配置。
为此,我们使用 Python、xml.etree 和 SQLAlchemy(将 SQL 从您的程序中分离出来),因为我们只需很少的努力和很大的灵 active 即可启动和运行。
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
要进行更改,您可以更正构建器或创建新构建器。您调整加载器源以确定将使用哪个构建器。您可以毫不费力地将 builder 的选择设为命令行参数。动态语言中的动态导入对我来说似乎有点矫枉过正,但它们很方便。
关于java - 哪种语言最容易和最快地处理 XML 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301493/