Skip to content

[j2x]: attrNodeName invalid behavior #67

@Delagen

Description

@Delagen

Checklist

Please fill below checklist

  • Are you running the latest version?
  • Have you include sample input?
  • Have you include actual output?
  • Have you include expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?
  • Did you bookmarked the repository for further updates? ;)

Input data

{
					root: {
						element:  {
							$: {
								aaa: "aaa",
								bbb: "bbb"
							},
							_: 1
						},
						element2: {
							$:          {
								aaa: "aaa2",
								bbb: "bbb2"
							},
							subelement: {$: {aaa: "sub_aaa"}}
						},
                                                date:"test"
					}
				}

Code

var options= {
	attributeNamePrefix: "",
	attrNodeName:        "$",
	textNodeName:        "_",
	ignoreAttributes:    false,
	cdataTagName:        "$cdata",
	cdataPositionChar:   "\\c",
	format:              false,
	indentBy:            "\t",
	supressEmptyNode:    true
};
var Parser = require("fast-xml-parser").j2xParser;
var result = new Parser(options).parse(obj);

Output data

<root date="test">
<element aaa="aaa" bbb="bbb" _="1"/>
<element2 aaa="aaa2" bbb="bbb2">
<subelement aaa="sub_aaa"/>
</element2>
</root>

expected data

<root>
<element aaa="aaa" bbb="bbb">1</element>
<element2 aaa="aaa2" bbb="bbb2">
<subelement aaa="sub_aaa"/>
</element2>
<date>test</date>
</root>

I think in j2x.js
should be as

function isAttribute(name/*, options*/) {
    if (!this.options.attrNodeName && name.startsWith(this.options.attributeNamePrefix)) {
        return name.substr(this.attrPrefixLen);
    } else {
        return false;
    }
}

or something else

Also condition should be removed in x2j due deprecation

if(options.decodeHTMLChar)
matches[i][4] = options.attrValueProcessor(matches[i][4]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions