简体   繁体   中英

lxml parse XML in python between 2 database using xpath function how to ?

So, here's my game plan .....

xpath in python 在此处输入图片说明

Here's my xml

So this xml is stored in a database ( ca ) , I need to extract this "data" to get all these value layer out and store them to another database ( a ) :

Here's what's I came up so far..

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

.. everything work out , except after fetchall() I was able to get ('//text') : but how can I use Xpath to go into specific node to get value or text from this example ?

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

Thanks Andrean! Just in case someone need that answer. This works!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM