-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Not really a feature request after all, more of a I'm stupid and realized XMLBuilder2 is awesome, doing exactly what I thought it didn't.~~question: How to convert existing JSON to XML?~~ ~~Feature Request: Compatibility with xq/yq~~ #129
Comments
Also, comparing that XML above converting to JSON (piped using xq), using XMLBuilder2 to convert the XML to JSON object:
shows as almost the exact same thing, other than mediawiki.page.revision.text['#'] instead of mediawiki.page.revision.text['#text'], and same # vs #text in namespaces This is actually much better than I initially noticed when reading documentation at https://npmjs.com/package/xmlbuilder2 but also I didn't realize converting XML to JSON, and I was looking more into how to convert the existing JSON I have into XML, to exactly reproduce the source XML file. And I initially was thinking that XMLBuilder2 would be able to convert the JSON that I have already, into XML, but I don't see how to do that. |
I figured out how to convert JSON to XML:
and the output is perfect! Even adding |
FYI, the full documentation is here: https://oozcitak.github.io/xmlbuilder2/ |
I'll just stick this in here: I glanced at https://en.wikipedia.org/wiki/XML#Key_terminology and noticed xmlbuilder2.convert(json, { encoding: 'utf8', prettyPrint: true }); thinking maybe the output would appear as |
Aha! As learned from #130 (comment) the encoding option is only able to be input in structure that is recognized as BuilderOptions order of argument evaluation. Therefore, this code works to set the encoding value:
|
Explained here also: https://oozcitak.github.io/xmlbuilder2/builder-functions.html#convert |
Is your feature request related to a problem? Please describe.
I am using command line
xq
to convert XML file to JSON, and I would like to convert that JSON back to exact same XML using Node.js becauseyq -x
returns "yq: Error running jq: ReaderError: unacceptable character #x0081: control characters are not allowed" in some cases working with data that should be preserved, if possible. The JavaScript object structure provided byxq
is preferable over the layout generated by xmlbuilder2, or at least I'd like to not have manipulate the JavaScript object generated byxq
to conform to xmlbuilder2 standards just output it back to the exact same XML that was initially processed (for my initial attempt to ensure smooth operation)Describe the solution you'd like
I would like to use existing JavaScript object structure generated by existing XML<->JS Linux software applications to be easily manageable with Node.js swap-in replacements.
Describe alternatives you've considered
I can't keep track of the various different Node.js scripts I've encountered throughout the last month or two, but I haven't found an ideal Node.js solution to convert XML to JSON and JSON back to XML, and provide seamless functionality, especially preserving data without manipulation/modification (all sorts of data from Wikipedia XML dumps for Templates and Modules and other namespaces data). Some of the libraries didn't recognize the @ system in attribute names, or otherwise organize attributes into JS object in hierarchical locations that are not standard across other libraries, making interoperability to work with converting back and forth difficult and nonseamless.
Additional context
Also brief example:
Input XML (e.g. from Wikipedia export @ https://en.wikipedia.org/wiki/Special:Export):
Generated JavaScript Object (JSON) from
cat "${file}"|xq
:Note that mediawiki.page becomes an array instead of an object if more than one child element, like:
Also, I prepared this with consideration of reaching out to other related XML<->JSON projects to possibly suggest similarly to each, so that possibly other projects may also consider similar interoperability considerations, if possible.
The text was updated successfully, but these errors were encountered: