如何将XMLstring与由objectify创build的XML合并?

我正在使用Python 2.7

我目前有一个适当的程序,从csv数据生成的XML的订单。 然而,一切都是硬编码的,我想让它更具dynamic性,因为我扩展了代码以适应更多的客户端。

现在,我有一个部分添加了对订单处理和发票等基本信息

from lxml import objectify E = objectify.E fileElem = E.request( E.customerID("###"), E.userID("####"), E.btNameCompany("BillToCompany"), E.btAttention("John Snow"), E.btStreet("123 Any Street"), E.btAddress2(), E.btAddress3(), E.btCity("City"), E.btState("State"), E.btZip("12345"), E.btCountry("USA"), E.btTelephone(), E.btEmail(), E.customerPO(customerPO), E.stNameCompany(shipToCompany), E.stAttention(shipToAttn), E.stStreet(shipToAddr), E.stAddress2(shipToAddr2), E.stCity(shipToCity), E.stState(shipToState), E.stZip(shipToZip), E.stCountry(shipToCountry), E.shipMethod("FedEx Ground"), E.stTelephone(shipToPhone), E.shipNotificationEmail(shipToEmail), E.shipperID("ShipperCompanyID"), E.messages(), ) 

从那里我一直在附加产品信息通过调用一个函数返回一个产品build立相同的方式,与硬编码标签。

 def getProductXML(sku, qty): productList = {"Company Brochure": E.item( E.quantity(qty), E.productID("ID #"), E.productDesc("Description") ), "Company Product": E.item( E.quantity(qty), E.productID("ID #"), E.productDesc("Description") ),} return productList[sku] fileElem.append(getProductXML(sku, qty)) 

我做了一些调整,允许我通过设置一个excel文件来添加特定项目需要的任何标签。 每个项目占用两行。 一行是标签,另一行是内容,第一列中的sku作为标识符。

 sku | qty | product ID | random attribu | random attribu2 sku | 1 | thing1 | English | z fold 

所以我添加了一些运行在excel文档中的代码,并以stringforms生成XML。 我select创build一个string,因为在objectify中运行一个循环会导致错误。 我也无法使用variables作为标签,所以如果variable =“quantity”,则variablesE.variable(qty)变成<variable>qty</variable>而不是<quantity>qty</quantity>

 from lxml import etree as ET def GetItemXML(sku,qty) def HeaderRows(r_sheet, rows, cols ): headerRows = {} for row in range(0,rows): if row%2 == 0: headerRow = [] for col in range(0,cols): if col == 0: thecell=r_sheet.cell(row-1,col) headerSKU = str(thecell.value) else: thecell=r_sheet.cell(row,col) header = str(thecell.value) headerRow.append(header) headerRows[headerSKU] = headerRow else: continue return headerRows def ContentRows(r_sheet, rows, cols ): contentRows = {} for row in range(0,rows): if row%2 == 1: contentRow = [] for col in range(0,cols): if col == 0: thecell=r_sheet.cell(row,col) contentSKU = str(thecell.value) else: thecell=r_sheet.cell(row,col) content = str(thecell.value) contentRow.append(content) contentRows[contentSKU] = contentRow ## print contentSKU else: continue return contentRows def getItemXML(sku, qty, contentRows, headerRows): skuExists = True string = "" ##################################### INSERT LOGIC TO ADD PROPER PRICING, QTY, AND SHIPPING WEIGHT BASED ON QTY ENTERED #################################### try: columnIndex = 0 for column in headerRows[sku]: string = string + ET.tostring(E(column, contentRows[sku][columnIndex])) columnIndex +=1 except: skuExists = False return string, skuExists 

这将输出以下string

 '<quantity>1</quantity><numPages>1</numPages><padding>0</padding><versions>0</versions><price>$$.$$</price><productID>ProdID#</productID><productDesc>Product Description</productDesc><device>0</device><inksF>4</inksF><inksB>0</inksB><runW>##.00000</runW><runH>##.00000</runH><cutW>##.00000</cutW><cutH>##.00000</cutH><productType>Inventory</productType><coatingID>0</coatingID><foldingID></foldingID><scoreID>0</scoreID><bindID>0</bindID><proofID>0</proofID><mailID>0</mailID><listID>0</listID><customerDataListID>0</customerDataListID><note></note><memo></memo><weight>16.0</weight><isCanvas></isCanvas><artID>28933</artID><mapingID>0</mapingID><customerApproval>accepted</customerApproval>' 

但是我不能把这个string转换回XML

 >>> root = objectify.fromstring(string) Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> root = objectify.fromstring(string) File "lxml.objectify.pyx", line 1802, in lxml.objectify.fromstring (src/lxml/lxml.objectify.c:22312) File "lxml.etree.pyx", line 3032, in lxml.etree.fromstring (src/lxml/lxml.etree.c:68292) File "parser.pxi", line 1786, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102641) File "parser.pxi", line 1674, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101470) File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96652) File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91461) File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92647) File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91943) XMLSyntaxError: Extra content at the end of the document, line 1, column 14 >>> 

要么

 >>> ET.XML(string) Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> ET.XML(string) File "lxml.etree.pyx", line 3012, in lxml.etree.XML (src/lxml/lxml.etree.c:68047) File "parser.pxi", line 1786, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102641) File "parser.pxi", line 1674, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101470) File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96652) File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91461) File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92647) File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91943) XMLSyntaxError: Extra content at the end of the document, line 1, column 14 

有人可以请推荐一种方法来结合xmlstringforms与我通过对象生成的XML?

谢谢。

您在上面发布的输出string不是有效的XML。 XML只允许有一个根元素,但是你的string有多个根元素,就像:

 <quantity>1</quantity> <numPages>1</numPages> <padding>0</padding> ..... ..... 

一种可能的解决方法是将string包装在单个根元素中,例如:

 valid_xml_string = '<root>' + string + '</root>' root = objectify.fromstring(valid_xml_string)