https://github.com/olaven/serialize-xml
Serialize (i.e. stringify) an object into xml string.
https://github.com/olaven/serialize-xml
deno xml
Last synced: 10 months ago
JSON representation
Serialize (i.e. stringify) an object into xml string.
- Host: GitHub
- URL: https://github.com/olaven/serialize-xml
- Owner: olaven
- License: lgpl-3.0
- Created: 2020-03-20T23:07:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T19:34:31.000Z (over 3 years ago)
- Last Synced: 2024-12-09T16:51:08.295Z (over 1 year ago)
- Topics: deno, xml
- Language: TypeScript
- Size: 434 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# serialize-xml  [](https://doc.deno.land/https/raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts) [](https://codebeat.co/projects/github-com-olaven-serialize-xml-master)
* A simple module for serializing objects to XML.
* Compatible with [Deno](deno.land) and [NodeJS](https://nodejs.org).
If you or your company is benefitting from `serialize-xml`, consider becoming a [sponsor](https://github.com/sponsors/olaven/).
This way I can work on new features and continue to maintain it worry-free.
## Usage
There is _one_ concept to understand:
* `Tag` - representing a tag, like ``
## Examples
```ts
//"serialize-xml" if you're using node
import { serialize, tag } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts"
//
const without_children = serialize(tag("name"))
//children
const without_attributes = serialize(tag("name", "children"))
//children
const full_tag = serialize(tag("name", "children", [["key", "value"]]))
const xml = serialize(
tag("outer",
[
tag("inner", "content")
],
[
["key", "value"]
]
)
);
//prints: content
console.log("serialized: ", xml);
```
### Serialze XML declarations
```ts
//"serialize-xml" if you're using node
import { serialize, declaration } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts";
const xml = serialize(declaration([["version", "1.0"]]));
//prints:
console.log("serialized declaration", xml);
```
### Multiple parents
```typescript
/*
*/
const xml = serialize(
declaration([["version", "1.0"]]),
tag("first_parent"),
tag("second_parent", [
tag("child")
])
);
```
### Alternatively, build tags by passing an object.
```ts
//"serialize-xml" if you're using node
import { serialize } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts"
const xml = serialize({
name: "my_tag_name",
children: [
{
name: "sub_tag",
children: "inner_content_of_tag",
attributes: [
["attribute_key", "attribute_value"]
]
}
],
attributes: []
});
//prints: 'inner_content_of_tag'
console.log("serialized: ", xml);
```
## Escaping
`serialize-xml` will escape tag values and tag attributes
according to [the XML spec](https://www.w3.org/TR/xml/).