GithubHelp home page GithubHelp logo

Comments (12)

 avatar commented on July 4, 2024 1

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.

julkue avatar julkue commented on July 4, 2024

that i'm not seeing

What do you mean with that?

from xmltojson.

andystocks avatar andystocks commented on July 4, 2024

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.

julkue avatar julkue commented on July 4, 2024

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.

andystocks avatar andystocks commented on July 4, 2024

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.

julkue avatar julkue commented on July 4, 2024

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.

andystocks avatar andystocks commented on July 4, 2024

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.

julkue avatar julkue commented on July 4, 2024

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.

andystocks avatar andystocks commented on July 4, 2024

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.

julkue avatar julkue commented on July 4, 2024

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.

andystocks avatar andystocks commented on July 4, 2024

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.

andystocks avatar andystocks commented on July 4, 2024

Thanks @MetaTribal @julmot

from xmltojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.