如何将 XML 字符串与 objectify 创建的 XML 合并?

How to merge XML string with XML created by objectify?

我正在使用 python 2.7

我目前有一个程序可以根据有效的 csv 数据在 XML 中生成订单。然而,一切都是硬编码的,我想在扩展代码以适应更多客户时使其更具动态性。

就目前而言,我有一个部分添加了基本信息,例如订单的发货地址和账单地址

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(),
    )

从那里我一直通过调用一个函数来附加产品信息,returns 一个产品以相同的方式构建,带有硬编码标签。

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 文档并生成 XML 作为字符串。我选择创建一个字符串,因为 运行 objectify 中的循环导致了错误。我也无法使用变量作为标签,所以如果变量 = "quantity",E.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

这将输出以下字符串

'<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>'

但是我无法将此字符串转回 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

有人可以推荐一种方法,将字符串形式的 xml 与我通过 objectify 生成的 xml 结合起来吗?

谢谢。

您在上面发布的输出字符串无效 XML。 XML 只允许有一个根元素,但是你的字符串有多个根元素,比如 :

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

解决此问题的一种可能方法是将字符串包装在单个根元素中,例如:

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