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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:19:09+00:00 2026-06-09T05:19:09+00:00

I’m using lxml to parse and objectify xml files in a path, I have

  • 0

I’m using lxml to parse and objectify xml files in a path, I have a lot of model and xsd’s, each object model maps to certain defined classes, for example if xml starts with model tag so it is a dataModel and if it starts with page tag it is a viewModel.

My question is how to detect in efficient way that xml file starts with which tag and then parse it with an appropriate xsd file and then objectify it

files = glob(os.path.join('resources/xml', '*.xml'))
for f in files:
    xmlinput = open(f)
    xmlContent = xmlinput.read()

    if xsdPath:
        xsdFile = open(xsdPath)
        # xsdFile should retrieve according to xml content
        schema = etree.XMLSchema(file=xsdFile)

        xmlinput.seek(0)
        myxml = etree.parse(xmlinput)

        try:
            schema.assertValid(myxml)

        except etree.DocumentInvalid as x:
            print "In file %s error %s has occurred." % (xmlPath, x.message)
        finally:
            xsdFile.close()

    xmlinput.close()
  • 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-09T05:19:11+00:00Added an answer on June 9, 2026 at 5:19 am

    I leave aside voluntarily file reading and treatments, to concentrate on your problem:

    >>> from lxml.etree import fromstring
    >>> # We have XMLs with different root tag
    >>> tree1 = fromstring("<model><foo/><bar/></model>")
    >>> tree2 = fromstring("<page><baz/><blah/></page>")
    >>>
    >>> # We have different treatments
    >>> def modelTreatement(etree):
    ...     return etree.xpath('//bar')
    ...
    >>> def pageTreatment(etree):
    ...     return etree.xpath('//blah')
    ...
    >>> # Here is a recipe to read the root tag
    >>> tree1.getroottree().getroot().tag
    'model'
    >>> tree2.getroottree().getroot().tag
    'page'
    >>>
    >>> # So, by building an appropriated dict :
    >>> tag_to_treatment_map = {'model': modelTreatement, 'page': pageTreatment}
    >>> # You can run the right method on the right tree
    >>> for tree in [tree1, tree2]:
    ...     tag_to_treatment_map[tree.getroottree().getroot().tag](tree)
    ...
    [<Element bar at 0x24979b0>]
    [<Element blah at 0x2497a00>]
    

    Hope this will be useful to someone, even if I had not seen this earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.