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

New feature: Convert JSON to XML #9

Closed
zhengchun opened this issue Jul 6, 2022 · 1 comment
Closed

New feature: Convert JSON to XML #9

zhengchun opened this issue Jul 6, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@zhengchun
Copy link
Contributor

This is feature JSON TO XML() already exists in package before. So this is not a new feature, this improve and remove anonymouse element appear in XML.

func (n *Node) OutputXML() string {

{
  "name":"John",
  "age":31, 
  "female":false 
}

Before

doc, _ := jsonquery.Parse(strings.NewReader(s))
fmt.Println(doc.OutputXML())

// Output:

<?xml version="1.0"?>
<age>31</age>
<female>false</female>
<name>John</name>

The above is not valid XML.

Expected

<?xml version="1.0"?>
<root>
  <age>31</age>
  <female>false</female>
  <name>John</name>
</root>

for array in JSON

{
		"name":"John",
		"age":26,
		"address":{
		   "streetAddress":"naist street",
		   "city":"Nara",
		   "postalCode":"630-0192"
		},
		"phoneNumbers":[
		   {
			  "type":"iPhone",
			  "number":"0123-4567-8888"
		   },
		   {
			  "type":"home",
			  "number":"0123-4567-8910"
		   }
		]
	 }

Before

<?xml version="1.0"?>
<address>
	<city>Nara</city>
	<postalCode>630-0192</postalCode>
	<streetAddress>naist street</streetAddress>
</address>
<age>26</age>
<name>John</name>
<phoneNumbers>
	<element>
		<number>0123-4567-8888</number>
		<type>iPhone</type>
	</element>
	<element>
		<number>0123-4567-8910</number>
		<type>home</type>
	</element>
</phoneNumbers>

After

<?xml version="1.0" encoding="UTF-8" ?>
<root>
	<name>John</name>
	<age>26</age>
	<address>
		<streetAddress>naist street</streetAddress>
		<city>Nara</city>
		<postalCode>630-0192</postalCode>
	</address>
	<phoneNumbers>
		<type>iPhone</type>
		<number>0123-4567-8888</number>
	</phoneNumbers>
	<phoneNumbers>
		<type>home</type>
		<number>0123-4567-8910</number>
	</phoneNumbers>
</root>
@zhengchun zhengchun added the enhancement New feature or request label Jul 6, 2022
zhengchun added a commit that referenced this issue Jul 7, 2022
zhengchun added a commit that referenced this issue Jul 7, 2022
@zhengchun
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant