Comments (12)
One of the design goals for this library was to provide a robust and consistent parsing of the xml. That includes the ability to cope with nested children, attributes, namespaces, namespaced attributes, etc. In order to accommodate all these different cases, I chose to use type identifiers (_text, _attr, _value, etc) for certain node types and to enforce their use for consistency. You can change the values of these JSON keys using the options object as well as force objects instead of arrays (but you will lose explicit ordering).
If that doesn't work for you, you might look into x2js, https://github.com/abdmob/x2js, which provides some different parsing options.
from xmltojson.
that i'm not seeing
What do you mean with that?
from xmltojson.
What are you mean with that?
I'm asking if I don't know of an option to remove the tag name attribute? Basically, in the JSON example provided at the beginning of the thread I need to remove "_text"
attribute
from xmltojson.
I'm asking if I don't know of an option to remove the tag name attribute?
This sentence makes no sense for me, sorry.
If you want to delete an object key you can use delete obj["_text"]
.
from xmltojson.
This sentence makes no sense for me, sorry.
I understand what you mean now. Just to be clear I know nothing on JSON, its the first time I'm working with it, so excuse me if I dont use specific vocabulary.
I'll go back to the first bit of code I used.
When I parse the XML:
<xml> <a>It Works!</a> </xml>
I get JSON (stringified)
{ "xml": [{ "a": [{ "_text": "It Works!" }] }] }
When I really need to get the JSON
{ "xml": [{ "a": "It Works!" }] }
without the "_text" attribute in it
from xmltojson.
Let's keep in your example: What would you do then if your <a>
element has an attribute like <a id="test">It works! <a>
? How would the JSON structure look then in your example?
from xmltojson.
What would you do then if your element has an attribute like It works! ? How would the JSON structure look then in your example?
I think I see what you mean now, according to the documentation it should look something like
{ "xml": [{ "a": [{ "_attr": { "id": { "_value": "test" } }, "_text": "It works!" }] }] }
I won't provide XMLs with attributes in them, only with elements - in that case i think it would be possible to get the "_text" out and just get the desired "a": "It Works!"
Im really sorry if im not getting through with the explanation, thanks for your help!
from xmltojson.
Ok, let's keep in your example: What would you do if your <a>
element contains children? How would the JSON structure look then in your example?
from xmltojson.
In the XML files I will be passing, the child elements won't contain any data, but the parent tag names would have to be represented in the JSON file.
from xmltojson.
If you have more than one value (the text) to display, e.g. because of childrens or attributes, you won't be able to have a structure like in your example. Most people have XML files that have at least one of them.
If you are confused about the name of the element ("_text") you can change this related to the documentation.
from xmltojson.
Thanks for your help @julmot , clearly there is something I'm not getting right
I just don't understand why this XML:
<xml> <1> <a>one</a> </1> <2> <b>two</b> </2> </xml>
can't be converted to a JSON like this
[{ "xml": { "1":[ { "a":"one" } ] "2":[ { "b":"two" } ] } }]
I'll leave the issue open just for a few hours to see if I get help from anyone else
from xmltojson.
Thanks @MetaTribal @julmot
from xmltojson.
Related Issues (20)
- Add license in bower.json and .min.js HOT 1
- Update module on npm HOT 7
- Cannot iterate children at proper order HOT 7
- When attribute name of an element have ":" character, does not parse HOT 1
- oXMLParent.hasChildNodes is not a function HOT 1
- Add the proper require() statement to your Readme.md file HOT 1
- Internal state (options) can be modified asynchronously HOT 2
- Cannot get it working with latest angular-cli with webpack HOT 4
- Doesn't work in NodeJS HOT 5
- syntax error display on console when call xml_str2json() on firefox HOT 2
- How to convert XML to JSON in an external reference HOT 3
- How to force to dispaly 0 after decimal? HOT 1
- Implement promises for asynchronous parsing of XML
- Improve examples in README
- Improve test coverage
- Migrate to latest Jasmine (2.8.x)
- Add support for processing instructions and comments HOT 2
- Migrate to DOM4
- Cannot parse Android Manifest XML HOT 4
- Support node v20.13.1 or latest
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmltojson.