Python Forum
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Xpath queries
#1
Hello

I am able to write some python code, including functions. Thanks for the help

I want to write xPath Queries using Python. I need to analyze large set of xml files.

Can someone please guide me where to start or some samples?

Thanks
Sairam
Reply
#2
You can find packages here: https://pypi.python.org/pypi?%3Aaction=s...mit=search
many will come with example code, or you may find some applications which use a particular package here: http://nullege.com/
Reply
#3
Thanks
Reply
#4
Take a look a this tutorial.
So there use BS and lxml,both work fine for parse xml.
No is it html parsing in tutorial,but not so much difference for xml.

For using xPath is lxml a good choice.
Example:
from lxml import etree

# Simulate xml
xml = '''\
<bookstore>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>20013</year>
    <price>49.99</price>
    <year>2015</year>
    <price>30</price>
  </book>
</bookstore>
'''

tree = etree.XML(xml)
Test parse with xPath:
>>> x_path = tree.xpath("//title[@lang='en']")
>>> x_path[0].text
'XQuery Kick Start'

>>> x_path = tree.xpath("/bookstore/book[1]/year")
>>> x_path[0].text
'20013'
>>> x_path[1].text
'2015'

>>> x_path = tree.xpath(".//price")
>>> [float(i.text) for i in x_path]
[49.99, 30.0]
Reply
#5
I am looking for xml parsing Xpath queries.

Can someone recommend me a simple tutorial for xpath queries to parse xml (no youtube video please)

SaiRam
Reply
#6
google 'simple tutorial for xpath queries' you'll be surprised what you'll find
Reply
#7
Please refrain from using private mail. The forum is for all to see.
one of the google links was: https://www.codeproject.com/Articles/189...e-to-XPath
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too many queries? lorasf 6 988 Jul-04-2023, 04:27 AM
Last Post: lorasf
  Text File Manipulation Queries? JustJeff 2 2,121 Apr-10-2021, 08:12 PM
Last Post: JustJeff
  2 queries how to use "row" zxcv 1 1,954 Oct-30-2018, 04:19 AM
Last Post: nilamo
  Pyhton and SQL Lite3 Data Queries compumarsh 4 3,096 Sep-21-2018, 02:29 PM
Last Post: compumarsh
  How Does pyspark deal with Spaces in Queries cpatte7372 3 2,923 Jul-31-2018, 09:53 PM
Last Post: micseydel
  Slow execution of MS Access queries Anirudh_Avantsa 4 4,378 Feb-12-2018, 05:01 PM
Last Post: nilamo
  Problem with Python, MySQL and Multi-threading queries zagk 1 11,898 Jul-01-2017, 12:15 AM
Last Post: zagk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020