Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 604 forks source link

xml to json And json to xml #341

Open starhao opened 7 years ago

starhao commented 7 years ago

i want parse a xml file , change a value, then save...there will happen a problem.. For example, file's content is :

 .....
<dict>
    <key>bundle-identifier</key>
    <string>xxx.xxx.xxxxxxx</string>
    <key>bundle-version</key>
    <string>1.1.1</string>
    <key>kind</key>
    <string>software</string>
    <key>title</key>
    <string>xxxx</string>
</dict>
......

code :

parseString(content, function (err, result) {
            console.log(JSON.stringify(result));
            var builder = new xml2js.Builder();
            var xmlResult = builder.buildObject(result);
            console.log(xmlResult);
        });

the xmlResult's content:

.....
<dict>
          <key>bundle-identifier</key>
          <key>bundle-version</key>
          <key>kind</key>
          <key>title</key>
          <string>xxx.xxx.xx</string>
          <string>1.1.1</string>
          <string>software</string>
          <string>xxxxxxx</string>
</dict>
.....

why ? and i how to do ? thanks ...

maa105 commented 7 years ago

thats apperently how it works you can provide some options for the parser to preserve order like so: parser = new xml2js.Parser({ explicitChildren: true, preserveChildrenOrder: true, charsAsChildren: true }); but unfortunately the builder fails to put them together correctly like at all, and also the json output is a salad of different formats :( +1 for fixing this issue

lachlanmcdonald commented 7 years ago

If you are parsing Property List files (plists) maybe the plist module is a better fit.

mesutyigit commented 6 years ago

@maa105 how to reconstruction json -> xml
after used new xml2js.Parser({ explicitChildren: true, preserveChildrenOrder: true, charsAsChildren: true });

gggiiia commented 4 years ago

nothing on this? i mean it has been 4 years and the issue is still there, cannot take an xml input, preserve its node order, get a damn json, and put the xml back together

Leonidas-from-XIV commented 4 years ago

Nothing on this I'm afraid. I mean, it has been 4 years and still nobody pays me to work on xml2js. My current schedule does not leave me enough time to do a proper rework of the code to parse it in a way where it is properly roundtripable.