0

所以,这是我的游戏计划......

python中的xpath在此处输入图像描述

这是我的xml

所以这个 xml 存储在一个数据库(ca)中,我需要提取这个“数据”来获取所有这些值层并将它们存储到另一个数据库(a):

这是我到目前为止想到的..

import pyodbc
from lxml import etree
from StringIO import StringIO


con_ca = pyodbc.connect(..)
con_a  = pyodbc.connect(..)

cur_ca = con_ca.cursor()
cur_c = con_c.cursor() 

cur_ca.execute("""
select id_original,data
from table
""")

rows_ca = cur_ca.fetchall()
for row in rows_ca:

     id_original = id_original

     x = str(row.data)
     root = etree.fromstring(x)  

     BValid   = etree.XPath('/Data/Response/Detail/B/Valid')  
     BPass    = etree.XPath('/Data/Response/Detail/B/Pass')  
     BDetails = etree.XPath('/Data/Response/Detail/B/Details')  
     BCode    = etree.XPath('/Data/Response/Detail/B/Code')  
     BDecisionS = etree.XPath('/Data/Response/Detail/B/Decision/Result') 
     BDecisionB = etree.XPath('/Data/Response/Detail/B/Decision/Bucket') 



con_a.execute("""
INSERT INTO table2 (id_original,BValid,BPass,BDetails,BCode,BDecisionS BDecisionB) 
VALUES(?, ?, ?,?, ?, ?, ?)
""")

.. 一切正常,除了在 fetchall() 之后我能够得到 ('//text') :但是我如何使用 Xpath 进入特定节点以从这个示例中获取值或文本?

4

1 回答 1

0
(b_valid_text,) = root.xpath('/Data/Response/Detail/B/Valid/text()') 

谢谢安德烈!以防万一有人需要那个答案。这行得通!

于 2012-09-20T18:32:44.860 回答