NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.45k stars 296 forks source link

How to avoid generate "#text" field in json ? #571

Open summerCol opened 1 year ago

summerCol commented 1 year ago

Description

"fast-xml-parser": "^4.2.2": xml file as below without "#text" but parse json result contains "#text" field. How to avoid generate "#text" field in json ? ### Input
<?xml version="1.0" encoding="utf-8"?>
<VLAN Version="2.0">
  <Project Width="1920" Height="1080" Name="project_h01">
    <Page Width="1920" Height="1080" Name="page_3" BackgroundImage="photo\bg.png" Locked="False" ImageDisplayMode="Stretch">
    </Page>
    </Project>
</VLAN>

Code

const parseOptions = {
  attributeNamePrefix : "",
  // attrNodeName: "attr", //default is 'false'
  //textNodeName : "#text",
  ignoreAttributes : false,
  ignoreNameSpace : false,
  format: true,
  allowBooleanAttributes : false,
  parseNodeValue : false,
  parseAttributeValue : false,
  trimValues: false,
  cdataTagName: "__cdata", //default is 'false'
  cdataPositionChar: "\\c",
  parseTrueNumberOnly: false,
  numParseOptions:{
    hex: true,
    leadingZeros: true,
    //skipLike: /\+[0-9]{10}/
  },
  arrayMode: false, //"strict"
  attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
  tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a
  stopNodes: ["parse-me-as-string"],
  alwaysCreateTextNode: false
};
const parser = new XMLParser(parseOptions);
fs.readFile('./file/project_h01.xml', 'utf-8', (err, data) => {
    if (err) {
        console.error('read error', err.message);
    }
    console.log('file content is๏ผš ', data);
    let jObj = parser.parse(data);  
    console.log("xml-->json is:\n " + JSON.stringify(jObj, "", "\t"));
    //console.log("xml-->json is:\n " + JSON.stringify(jObj));
})

Output

{
    "?xml": {
        "version": "1.0",
        "encoding": "utf-8"
    },
    "VLAN": {
        "Project": {
            "Page": {
                "#text": "Page",
                "Width": "1920",
                "Height": "1080",
                "Name": "page_3",
                "BackgroundImage": "photo\\bg.png",
                "Locked": "False",
                "ImageDisplayMode": "Stretch"
            },
            "#text": "ProjectProject",
            "Width": "1920",
            "Height": "1080",
            "Name": "project_h01"
        },
        "#text": "VLANVLAN",
        "Version": "2.0"
    },
    "#text": "!xml"
}

expected data

{
    "?xml": {
        "version": "1.0",
        "encoding": "utf-8"
    },
    "VLAN": {
        "Project": {
            "Page": {
                "#text": "Page",
                "Width": "1920",
                "Height": "1080",
                "Name": "page_3",
                "BackgroundImage": "photo\\bg.png",
                "Locked": "False",
                "ImageDisplayMode": "Stretch"
            },
            "Width": "1920",
            "Height": "1080",
            "Name": "project_h01"
        },
        "Version": "2.0"
    }
}

Would you like to work on this issue?

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

github-actions[bot] commented 1 year ago

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

amitguptagwl commented 1 year ago

it looks like a bug. let me check it my own.

wackbyte commented 6 months ago

I think this is happening because the order of attrName/tagName and val are reversed in your function parameters. You are using (val, name) when it should be (name, val).