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

More friendly "@" and "#text", check for existing children in postprocess()? #326

Open
ferventgeek opened this issue Apr 11, 2023 · 1 comment

Comments

@ferventgeek
Copy link

ferventgeek commented Apr 11, 2023

My project is a wrapper on a clunky XML interface. Not SOAP, but you can see it from there. I'm trying to make it easy for users to access data using dot notation, but "@" is causing issues for them. foo.bar['@baz'] does work, but foo.bar.baz would be cleaner. Better, it would hide the XML source semantics from "modern" devs who decry such shenanigans.

The code below can convert attributes and text fields to something more normal, but I'd like to add a check to see if a child property already exists with the intended key, and then only if it's a dupe add the "@" prefix. Am I missing an obvious setting?

<foo>
   <bar baz="Hello">world!</bar>
</foo> 

Would produce

"foo": {
   "bar": {
      "baz": "Hello",
      "value": "world!"
   }
}

And in the case of "duped" attribute/children:

<foo>
   <bar baz="Hello">
      <baz>What</baz>
      <value>123</value>
      world!
   </bar>
</foo> 

Would produce

"foo": {
   "bar": {
      "@baz": "Hello",
      "baz": "What",
      "value": 123,
      "#text": "world!"
   }
}

I realize this is more complex than it sounds- order of parsing for example. I'm doing by iterating over the map and creating a new one after the fact, but if it's easy to skip that step then all the better.

Test function

def postprocessor(path, key, value):
    if key[0] == "@":
        return key[1:], value
    elif key == "#text":
        return "value", value
    else:
        return key, value
@KeithProctor
Copy link

Yes how do you get rid of the @ and # characters from being applied to the conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants