LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-26-2023, 10:26 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Rep: Reputation: 35
Question [XML] How to get sub-elements (nodes + texts)?


Hello,

Since I'm not getting the subscription e-mails, I can't ask in those tools' forums on Sourceforge.

I tried xidel and XMLStarlet as command line tools to parse XML files and grab everything (elements + text) below given nodes… to no avail: The tools only return the texts it finds, not the elements.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<gpx>
  <metadata>
    <name>Some name</name>
  </metadata>
  <trk>
    <name>Track 1</name>
    <trkseg>
      <trkpt lat="48.81782" lon="2.24906">
        <ele>37.5</ele>
      </trkpt>
      <trkpt lat="48.81784" lon="2.24906">
        <ele>37.5</ele>
      </trkpt>
    </trkseg>
  </trk>
  <trk>
    <name>Track 2</name>
    <trkseg>
      <trkpt lat="48.81782" lon="2.24906">
        <ele>37.5</ele>
      </trkpt>
      <trkpt lat="48.81784" lon="2.24906">
        <ele>37.5</ele>
      </trkpt>
    </trkseg>
  </trk>
</gpx>
Code:
xidel -s input.gpx -e "//trk/trkseg/*"
xml sel -t -v "//trk/trkseg/*" input.gpx
Code:
37.5
37.5
37.5
37.5
Am I doing it totally wrong, or are those tools precisely meant to ignore elements themselves?

Thank you.

--
Edit: Yes, indeed. By default, only texts within elements are output.

Code:
xidel input.gpx -e "//trkseg/*/outer-xml(.)"

Last edited by littlebigman; 05-26-2023 at 11:11 AM.
 
Old 05-26-2023, 01:17 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,140
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
With your example as MyFile.xml

Example using xml.etree:
Code:
#!/usr/bin/python

from xml.etree import ElementTree
from urllib import request

#Make a user agent string for urllib to use
agent = ('Mozilla/5.0 (Windows NT 10.1; Win64; x64; rv:109.0) '
        'Gecko/20100101 Firefox/113.0')
        
user_agent = {'User-Agent': agent}

class MakeList():
    def __init__(self, url):
    
        #Get the xml to parse
        req = request.Request(url, data=None, headers=user_agent)
        html = request.urlopen(req)
        tree = ElementTree.parse(html)
        root = tree.getroot()
        
        #Get tag data     
        for i in root.iter('trkpt'):
            print(i.attrib)
        
if __name__ == "__main__":

    #Local file or remote url
    url = 'file:///path/to/MyFile.xml'
    MakeList(url)
Code:
python ./MyFile.py
{'lat': '48.81782', 'lon': '2.24906'}
{'lat': '48.81784', 'lon': '2.24906'}
{'lat': '48.81782', 'lon': '2.24906'}
{'lat': '48.81784', 'lon': '2.24906'}
 
Old 05-26-2023, 02:31 PM   #3
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Original Poster
Rep: Reputation: 35
Yes, it works fine with Python and ET/BS, but I needed a lighter solution, with just a single binary.

Another way to solve it:

Code:
xidel input.gpx -se "//trk/trkseg/*" --printed-node-format xml
Too bad it wasn't shown at the top of the readme to make it obvious instead of at the very bottom ("Only the string value of elements is printed, unless the --printed-node-format is set to XML or HTML. (E.g. <a>bc</a> only prints "bc")").
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up Kickstart server RHEL 6.5 for compute nodes, GPU nodes and PHI nodes sho1sho1 Red Hat 3 06-23-2015 04:20 PM
In Javascript How to replace elements in one object with elements from another object pizzipie Linux - Software 1 05-08-2014 02:28 AM
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
[SOLVED] XML editing: adding elements to xml lists alexwely Programming 7 04-20-2012 12:45 PM
How to get texts and image of texts on ubuntu terminal like linux mint? cola Ubuntu 1 06-02-2010 12:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration