Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks
summerCol opened this issue May 6, 2023 · 3 comments
Open
6 tasks

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

summerCol opened this issue May 6, 2023 · 3 comments
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation

Comments

@summerCol
Copy link

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

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?

  • Yes
  • No

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

@github-actions
Copy link

github-actions bot commented May 6, 2023

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
Copy link
Member

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

@amitguptagwl amitguptagwl added the Pending Pending to be confirmed by user/author for some check/update/implementation label May 17, 2023
@wackbyte
Copy link
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation
Projects
None yet
Development

No branches or pull requests

3 participants