要实现搜索XML页面并提取特定商品名称的订单的商品ID和客户ID,你可以使用Python的xml.etree.ElementTree
库来解析XML文件。以下是一个简单的示例代码:
import xml.etree.ElementTree as ET
def search_product_in_xml(xml_file, product_name):
tree = ET.parse(xml_file)
root = tree.getroot()
# 假设XML结构如下:
# <orders>
# <order>
# <customer_id>...</customer_id>
# <product>
# <product_id>...</product_id>
# <name>...</name>
# </product>
# </order>
# ...
# </orders>
results = []
for order in root.findall('order'):
product = order.find('product')
if product is not None and product.find('name').text == product_name:
customer_id = order.find('customer_id').text
product_id = product.find('product_id').text
results.append((customer_id, product_id))
return results
# 使用示例
xml_file_path = 'path_to_your_xml_file.xml'
product_name_to_search = '商品名称'
results = search_product_in_xml(xml_file_path, product_name_to_search)
for customer_id, product_id in results:
print(f'Customer ID: {customer_id}, Product ID: {product_id}')
通过以上代码和解释,你应该能够实现搜索XML页面并提取特定商品名称的订单的商品ID和客户ID。如果遇到其他问题,请提供具体错误信息以便进一步诊断。
领取专属 10元无门槛券
手把手带您无忧上云