Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple way to locate an element from a parsed xml file #1113

Open
dsayling opened this issue Jan 9, 2025 · 0 comments
Open

simple way to locate an element from a parsed xml file #1113

dsayling opened this issue Jan 9, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@dsayling
Copy link

dsayling commented Jan 9, 2025

As someone who parses large xml files, I would like a way to locate a specific element within the file based on the parsed model, so that I can report to users where there are issues within the large xml files.

I understand that this would require generating models that inherit from some type of base, or perhaps setting attributes on the built models as its parsed...

Possible ideas:

Given

from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
from tests.fixtures.primer import PurchaseOrder

config = ParserConfig()
context = XmlContext()
parser = XmlParser(context=context, config=config)

order = parser.parse("tests/fixtures/primer/sample.xml", PurchaseOrder)

Then

>>> order.bill_to._meta_.line_number
10

or

order.bill_to.get_xpath() # or something
# can then be used with element tree
etree.parse("tests/fixtures/primer/sample.xml").xpath(order.bill_to.get_xpath())[0].source_line
@tefra tefra added the enhancement New feature or request label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants