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

Add support for xpath and xml attr. via @ #66

Closed
JorgeCepeda opened this issue Feb 18, 2023 · 11 comments
Closed

Add support for xpath and xml attr. via @ #66

JorgeCepeda opened this issue Feb 18, 2023 · 11 comments

Comments

@JorgeCepeda
Copy link

JorgeCepeda commented Feb 18, 2023

In a project we're trying to interpret either Json or XML depending on what the user gives as entry data, along with some paths to the document.

We're using Jackson and the dataformat-xml and choose the mapper via a flag, but the path syntax doesn't seem to allow grabbing attributes with @, like in xpath syntax, example:
xml = "<a b=\"attrib\"><b>node</b></a>"

and as path: "/a/@b" for the attribute and "/a/b" for the node

@cowtowncoder
Copy link
Member

This would definitely need actual description of what is desired: title is quite ambiguous.

@JorgeCepeda
Copy link
Author

I updated the description with an example of what I'm trying to do, I see for Json one option is JsonPath

https://github.com/json-path/JsonPath
https://www.w3schools.com/xml/xpath_syntax.asp

@pjfanning
Copy link
Member

Maybe https://cassiomolin.com/2016/07/13/using-jackson-and-json-pointer-to-query-and-parse-an-arbitrary-json-node/ is worth a read. If you Google Jackson and JSON pointer or Path, you will find lots of existing solutions.

@JorgeCepeda
Copy link
Author

I'm able to read json, but I need to keep the node since the structure is arbitrary

@cowtowncoder
Copy link
Member

My main issue is that XPath is strongly XML-specific, ditto for attributes. It will not be usable for things other than XML, since concept of attributes (f.ex) are XML-isms.
As such I do not think JSON Pointer should have anything special beyond its (simple) specification.

But it does sound like this would be specific for Jackson XML format module (jackson-dataformat-xml), where it may or may not make sense. Jackson does not handle XML as DOM nodes, for example; and all logical manipulation occurs via streams of JsonTokens. Some information on attribute/element is retained, but not a lot.

At the same time, adding something to decorate attributes with @ prefix could be possible.

@JorgeCepeda
Copy link
Author

JorgeCepeda commented Feb 21, 2023

As long as there is some xml attribute syntax it would suffice since json and xml have their own mapper

@cowtowncoder
Copy link
Member

For suggestions for features to add to XML module, an issue would need to be created at

https://github.com/FasterXML/jackson-dataformat-xml/issues

@JorgeCepeda
Copy link
Author

JorgeCepeda commented Feb 22, 2023

Side note: what is currently the way to do node.at("/a/b").asText() for that xml?

@cowtowncoder
Copy link
Member

cowtowncoder commented Feb 22, 2023

The original case is not something Jackson supports, due to name collision (or, rather, I think both attribute b and element bs values would be combined, depending on target type -- for JsonNode become an array value).

But if there was a feature to use @ prefix, you'd use

node.at("/a/@b").asText();

for attribute and

node.at("/a/b").asText()

for element.

@JorgeCepeda
Copy link
Author

Okay, I'll open the issue soon then, thanks

@cowtowncoder
Copy link
Member

Filed as:

FasterXML/jackson-dataformat-xml#573

so closing this one.

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

3 participants