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 518971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:59:32+00:00 2026-05-13T07:59:32+00:00

I try to parse an xml file. The text which is in tags is

  • 0

I try to parse an xml file. The text which is in tags is parsed successfully (or it seems so) but I want to output as the text which is not contained in some tags and the following program just ignores it.

from xml.etree.ElementTree import XMLTreeBuilder

class HtmlLatex:                     # The target object of the parser
    out = ''
    var = ''
    def start(self, tag, attrib):   # Called for each opening tag.
        pass
    def end(self, tag):             # Called for each closing tag.
        if tag == 'i':
            self.out += self.var
        elif tag == 'sub':
            self.out += '_{' + self.var + '}'
        elif tag == 'sup':
            self.out += '^{' + self.var + '}'
        else:
            self.out += self.var
    def data(self, data):
        self.var = data
    def close(self):
        print(self.out)


if __name__ == '__main__':
    target = HtmlLatex()
    parser = XMLTreeBuilder(target=target)

    text = ''
    with open('input.txt') as f1:
        text = f1.read()

    print(text)

    parser.feed(text)
    parser.close()

A part of the input I want to parse:
<p><i>p</i><sub>0</sub> = (<i>m</i><sup>3</sup>+(2<i>l</i><sub>2</sub>+<i>l</i><sub>1</sub>) <i>m</i><sup>2</sup>+(<i>l</i><sub>2</sub><sup>2</sup>+2<i>l</i><sub>1</sub> <i>l</i><sub>2</sub>+<i>l</i><sub>1</sub><sup>2</sup>) <i>m</i>) /(<i>m</i><sup>3</sup>+(3<i>l</i><sub>2</sub>+2<i>l</i><sub>1</sub>) ) }.</p>

  • 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-13T07:59:33+00:00Added an answer on May 13, 2026 at 7:59 am

    Here’s a pyparsing version – I hope the comments are sufficiently explanatory.

    src = """<p><i>p</i><sub>0</sub> = (<i>m</i><sup>3</sup>+(2<i>l</i><sub>2</sub>+<i>l</i><sub>1</sub>) """ \
          """<i>m</i><sup>2</sup>+(<i>l</i><sub>2</sub><sup>2</sup>+2<i>l</i><sub>1</sub> <i>l</i><sub>2</sub>+""" \
          """<i>l</i><sub>1</sub><sup>2</sup>) <i>m</i>) /(<i>m</i><sup>3</sup>+(3<i>l</i><sub>2</sub>+""" \
          """2<i>l</i><sub>1</sub>) ) }.</p>"""
    
    from pyparsing import makeHTMLTags, anyOpenTag, anyCloseTag, Suppress, replaceWith
    
    # set up tag matching for <sub> and <sup> tags
    SUB,endSUB = makeHTMLTags("sub")
    SUP,endSUP = makeHTMLTags("sup")
    
    # all other tags will be suppressed from the output
    ANY,endANY = map(Suppress,(anyOpenTag,anyCloseTag))
    
    SUB.setParseAction(replaceWith("_{"))
    SUP.setParseAction(replaceWith("^{"))
    endSUB.setParseAction(replaceWith("}"))
    endSUP.setParseAction(replaceWith("}"))
    
    transformer = (SUB | endSUB | SUP | endSUP | ANY | endANY)
    
    # now use the transformer to apply these transforms to the input string
    print transformer.transformString(src)
    

    Gives

    p_{0} = (m^{3}+(2l_{2}+l_{1}) m^{2}+(l_{2}^{2}+2l_{1} l_{2}+l_{1}^{2}) m) /(m^{3}+(3l_{2}+2l_{1}) ) }.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm try to parse this xml, but c# keeps throwing an exception saying it
I try to parse articles from wikipedia. I use the *page-articles.xml file, where they
I try to parse an XML file with a StAX XML-parser. It give me
I am using opencsv to parse text file and generate List<String[]> now I want
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I am trying to parse a XML document using Xerces, but I cant seem
I have an xml file which I included as a resource in my netbeans
I have an xml, and I can't parse this file with xmlslurper. Here a
I have a function that reads certain tags out of an XML file. I
I've created a simple xml file here: http://roberthan.host56.com/productsNew.xml which is quite simple, the root

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.