Python使用XML读取多个内部标记

时间:2017-02-14 23:06:18

标签: python xml

我正在尝试使用python阅读以下XML代码。

<Product productCode="2" productCategory="ABC" productClass="SOMETHING" salable="true" statusCode="ACTIVE" outage="false">
    <PriceList>
        <Currency type ="NATIVE" symbol="US$">
            <Pricing priceCode="EATIN" catalogPrice="2.00" netPrice="2.00" tax="0.09" grossPrice="2.09"/>
        </Currency>
    </PriceList>
</Product>

我需要获得目录价格。

这是我的代码。我不知道如何获得目录价格。我想我不知道如何获得这些数据。任何帮助将不胜感激。

from xml.dom import minidom
doc = minidom.parse("US_2171_ProductPricing_20170206233707.xml")
# doc.getElementsByTagName returns NodeList
# name = doc.getElementsByTagName("name")[0]
# print(name.firstChild.data)
products = doc.getElementsByTagName("Product")
for product in products:
    productCodeID = product.getAttribute("productCode")
    statusCode = product.getAttribute("statusCode")  
    catalogPrice = pricing.getElementsByTagName("catalogPrice")
    print("productCode:%s , statusCode:%s, catalogPrice:%s" % (productCodeID, statusCode, catalogPrice))

2 个答案:

答案 0 :(得分:0)

你可以使用beautifulsoup库。这应该有效:

In file included from ../../singlestory.h:4:0,
                 from ../../volume.h:5,
                 from ../../dbinterface.h:8,
                 from ../../dbinterface.cpp:1:
../../scriptby.h:12:24: error: ‘static std::__cxx11::string ScriptBy::getTableName()’ cannot be declared
     static std::string getTableName();
                        ^
In file included from ../../dbinterface.h:7:0,
                 from ../../dbinterface.cpp:1:
../../persondb.h:15:25: error:   since ‘virtual std::__cxx11::string personDB::getTableName()’ declared in base class
     virtual std::string getTableName();

答案 1 :(得分:0)

我猜你已经弄明白了。只需获得每个产品的所有定价并循环使用它们。

for product in products:
    pricings = product.getElementsByTagName("Pricing")
    for pricing in pricings:
        print pricing.getAttribute("catalogPrice")