Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:44:17+00:00 2026-06-09T17:44:17+00:00

I’m reading instrument data from a specialty server that delivers the info in xml

  • 0

I’m reading instrument data from a specialty server that delivers the info in xml format. The code I’ve written is:
from lxml import etree as ET

xmlDoc = ET.parse('http://192.168.1.198/Bench_read.xml')
print ET.tostring(xmlDoc, pretty_print=True)

dmtCount = xmlDoc.xpath('//dmt')
print(len(dmtCount))

dmtVal = []

for i in range(1, len(dmtCount)):
    dmtVal[i:0] = xmlDoc.xpath('./address/text()')
    dmtVal[i:1] = xmlDoc.xpath('./status/text()')
    dmtVal[i:2] = xmlDoc.xpath('./flow/text()')
    dmtVal[i:3] = xmlDoc.xpath('./dp/text()')
    dmtVal[i:4] = xmlDoc.xpath('./inPressure/text()')
    dmtVal[i:5] = xmlDoc.xpath('./actVal/text()')
    dmtVal[i:6] = xmlDoc.xpath('./temp/text()')
    dmtVal[i:7] = xmlDoc.xpath('./valveOnPercent/text()')

print dmtVal

And the results I get are:

$python XMLparse2.py
<response>

<heartbeat>0x24</heartbeat>

<dmt node="1">

    <address>0x21</address>
    <status>0x01</status>
    <flow>0.000000</flow>
    <dp>0.000000</dp>
    <inPressure>0.000000</inPressure>
    <actVal>0.000000</actVal>
    <temp>0x00</temp>
    <valveOnPercent>0x00</valveOnPercent>

</dmt>

<dmt node="2">

    <address>0x32</address>
    <status>0x01</status>
    <flow>0.000000</flow>
    <dp>0.000000</dp>
    <inPressure>0.000000</inPressure>
    <actVal>0.000000</actVal>
    <temp>0x00</temp>
    <valveOnPercent>0x00</valveOnPercent>

</dmt>

</response>

...Starting to parse XML nodes
2
[]
...Done

Sooo, nothing is coming out. I’ve tried using /value in place of the /text() in the xpath call, but the results are unchanged. Is my problem:

1) An incorrect xpath command in the for loop? or

2) A problem in the way I’ve structured list variable dmtVal ? or

3) Something else I’m missing completely?

I’d welcome any suggestions! Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:44:19+00:00Added an answer on June 9, 2026 at 5:44 pm

    Counting <dmt/> tags and then iterating over them by index is both inefficient and un-Pythonic. Apart from that you are using wrong syntax (slice instead of index) for indexing arrays. In fact you don’t need to index the val at all, to do it Pythonic way use list comprehensions.

    Here’s a slightly modified version of what stranac suggested:

    from lxml import etree as ET
    
    xmlDoc = ET.parse('http://192.168.1.198/Bench_read.xml')
    print ET.tostring(xmlDoc, pretty_print=True)
    
    response = xmlDoc.getroot()
    
    tags = (
        'address',
        'status',
        'flow',
        'dp',
        'inPressure',
        'actVal',
        'temp',
        'valveOnPercent',
    )
    
    dmtVal = []
    
    for dmt in response.iter('dmt'):
        val = [dmt.xpath('./%s/text()' % tag) for tag in tags]
        dmtVal.append(val)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.