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

Something like CreateFromDocument or JAXBContext.newInstance? #332

Closed
mihxil opened this issue Nov 30, 2020 · 3 comments
Closed

Something like CreateFromDocument or JAXBContext.newInstance? #332

mihxil opened this issue Nov 30, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@mihxil
Copy link

mihxil commented Nov 30, 2020

If I want to parse XML it seems that I always have to specify the type:

XmlParser().from_string(data, <TYPE>)

In certain cases that would be overly restrictive. IIRC pyxb generates a method like CreateFromDocument, and e.g. in JAXB you can use a JAXBContext which you can configure with the namespaces to recognise.

Als to xsdata generated classes seem to contains sufficient information. Element-name and namespace of the root element generally is enough to know the type to create.

I may be missing something, but I don't see an easy way to accomplish this.

@tefra
Copy link
Owner

tefra commented Nov 30, 2020

In pyxb you still need to know the module with the generated models right and in jaxb you have to register the types manually beforehand.

The jaxb api seems interesting and easy to implement.

@tefra tefra added the enhancement New feature or request label Nov 30, 2020
tefra added a commit that referenced this issue Dec 1, 2020
@tefra
Copy link
Owner

tefra commented Dec 1, 2020

@mihxil give it a try #335, the type in now optional, as long as the models module is already imported the parser should be able to locate the target type.

XmlParser().from_string(data)

I need to add a few more tests and update the docs but its almost there.

tefra added a commit that referenced this issue Dec 2, 2020
tefra added a commit that referenced this issue Dec 2, 2020
tefra added a commit that referenced this issue Dec 2, 2020
tefra added a commit that referenced this issue Dec 2, 2020
tefra added a commit that referenced this issue Dec 2, 2020
Notes:
Its preferable to specify the target class
otherwise the xml context will scan all imported
modules to find the matching dataclass. The scan
result is cached, minimizing performance hit.

Closes #332
@tefra tefra closed this as completed in f1bdfdc Dec 2, 2020
@mihxil
Copy link
Author

mihxil commented Dec 4, 2020

Thanks, I'll try.

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