百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

node-jsontoxml

> 编程语言
开源

这使得一个简单的 JavaScript 对象结构成为了 xml/html. js 对象比字符串更容易修改, 因此不需要分析整个多姆来可靠地添加一个

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

这使得一个简单的 JavaScript 对象结构成为了 xml/html. js 对象比字符串更容易修改, 因此不需要分析整个多姆来可靠地添加一个

jsontoxml

This is a library designed to render js objects as xml. Its not made to parse or otherwise edit existing xml/html structures. For that and perhaps as a compliment to this you can use jsdom or xml2js for editing existing markup.

this will do a good job rendering json as xml but apis that require xml expect odd things mostly related to elements with attributes and implicit array like keys that make formatting your json a little tricky.

example

javascript
var jsonxml = require('jsontoxml');

var xml = jsonxml({
	node:'text content',
	parent:[
		{name:'taco',text:'beef taco',children:{salsa:'hot!'}},
		{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[
			{name:'salsa',text:'mild'},
			'hi',
			{name:'salsa',text:'weak',attrs:{type:2}}
		]},
		{name:'taco',attrs:'mood="party!"'}
	],
	parent2:{
		hi:'is a nice thing to say',
		node:'i am another not special child node',
		date:function(){
			return (new Date())+'';
		}
	}
})

console.log(xml);

outputs:

xml
text content

	
		beef taco
		hot!
	
	
		fish taco
		mild
		hi
		weak
	
	

	is a nice thing to say
	i am another not special child node
	Sun Sep 26 2010 17:27:29 GMT-0700 (PDT)

API

jsontoxml (obj,options)

  • a valid json structure to interpret or a json string
  • returns an xml string
    • options is optional. it can be true (add generic xml header) or an object. If an object, valid options are:
      • escape
        • calls escape on all values
        • attribute values if attribute values are specified as an object
      • xmlHeader can either be boolan (add generic `` header) or an object. If an object valid options are:
        • standalone if true, the `` gets an additional attribute standalone="yes".
      • docType if defined gets added as the `` contents (unescaped).
      • prettyPrint if truthy the output gets a rudimentary pretty print (good for debugging, don't expect too much)
      • indent specify what unit you would like to indent by (spaces, tabstop, nothing - pass an empty string)
      • removeIllegalNameCharacters replace illegal XML element Name characters with '_'
      • html instead of adding self closing tags for empty tags add an open and close tag. becomes

jsontoxml.escape (string)

  • returns string with xml entities escaped
  • escapes "" &

jsontoxml.cdata (string)

  • wraps string with ``
  • removes all occurences of close cdata (]]>) in input text

more description

I made this because i wanted to abstract away the fact that antiquated external systems require post data as xml and i wanted to expose a standard js calling api like my other interfaces.

I did not want to instantiate an entire dom to perform simple updates to tags in lower level functions (like injecting api keys) when top level api call specific functions start building the xml string.

Issues· 8 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言