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

  • Home
  • SEARCH
  • 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 6705549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:25:00+00:00 2026-05-26T07:25:00+00:00

I want to read the tag values like <title> , <title_id> from xml file.

  • 0

I want to read the tag values like <title>,<title_id> from xml file. The value of <title> read successfully. Is it possible to read the <title>,<title_id> with same loop?
Please help me I’m new to XML.

        <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.5/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.5/ http://www.mediawiki.org/xml/export-0.5.xsd" version="0.5" xml:lang="en">
      <siteinfo>
        <sitename>Wiki</sitename>
        <case>first-letter</case>
        <namespaces>
          <namespace key="0" case="first-letter" />
        </namespaces>
      </siteinfo>
      <page>
        <title>Sex</title>
        <title_id>31239628</title_id>
        <revision>
          <id>437708703</id>
          <timestamp>2011-07-04T13:53:52Z</timestamp>
          <text xml:space="preserve" bytes="6830">{{ Hello}}

    </text>
        </revision>
      </page>
    </mediawiki>

I’m using following code to read all the title from file. And its working fine.

import xml.etree.cElementTree as etree
tree = etree.parse('find_title.xml')
for value in tree.getiterator(tag='title'):
    print value.text
  • 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-05-26T07:25:00+00:00Added an answer on May 26, 2026 at 7:25 am

    If you are going to be working with XML a lot, I’d suggest you familiarise yourself with XPATH.

    Here’s a quick snippet using my XML library of preference, lxml.

    from lxml import etree
    
    doc = etree.XML("""
    <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.5/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.5/ http://www.mediawiki.org/xml/export-0.5.xsd" version="0.5" xml:lang="en">
      <siteinfo>
        <sitename>Wiki</sitename>
        <case>first-letter</case>
        <namespaces>
          <namespace key="0" case="first-letter" />
        </namespaces>
      </siteinfo>
      <page>
        <title>Sex</title>
        <title_id>31239628</title_id>
        <revision>
          <id>437708703</id>
          <timestamp>2011-07-04T13:53:52Z</timestamp>
          <text xml:space="preserve" bytes="6830">{{ Hello}}
          </text>
        </revision>
      </page>
    </mediawiki>
    """)
    
    def first(seq,default=None):
      for item in seq:
        return item
      return default
    
    NSMAP=dict(mw="http://www.mediawiki.org/xml/export-0.5/")
    
    print first(doc.xpath('/mw:mediawiki/mw:page/mw:title/text()',namespaces=NSMAP))
    print first(doc.xpath('/mw:mediawiki/mw:page/mw:title_id/text()',namespaces=NSMAP))
    

    Yields:

    Sex
    31239628
    

    Update – supposing multiple page elements

    XPATH queries mostly return node sequences (hence the first function).

    You could use a single query that returned the values of both tags for all of the pages. You would then have to group them together, if a subelement was missing from a page you’d be out of step. You could write the query to ensure the subelements existed, but you might want to know that there was a partial record, etc, etc.

    So my first answer to this would be to loop through the pages like so:

    for i,page in enumerate(doc.xpath('/mw:mediawiki/mw:page',namespaces=NSMAP)):
      title = first(page.xpath('./mw:title/text()',namespaces=NSMAP))
      title_id = first(page.xpath('./mw:title_id/text()',namespaces=NSMAP))
      print "Page %s: %s (%s)"  % (i,title,title_id)
    

    Yielding:

    Page 0: Sex (31239628)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read line n1->n2 from file foo.c into the current buffer. I
I want to read an specific xml node and its value for example <customers>
I want to read an xml file, apply a transform, then write to another
I want to read each line from a text file and store them in
I'm trying to read XML file, ex : <entry> <title>FEED TITLE</title> <id>5467sdad98787ad3149878sasda</id> <tempi type="application/xml">
I am using Eclipse. I want to read number of XML files from a
I've tried to read a big xml file (something like 500MB). First of all,
I have a text file. I want read that file. But In that if
I want to read and write from serial using events/interrupts. Currently, I have it
I want to read UTF-8 input in Perl, no matter if it comes from

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.