Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/bundler/loaders.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ XML files can be directly imported. Bun parses them with its native XML 1.0 pars

```ts
import doc from "./config.xml";
console.log(doc.config["@version"]);
console.log(doc);

// via import attribute:
import feed from "./export.rss" with { type: "xml" };
Expand Down
31 changes: 21 additions & 10 deletions docs/guides/runtime/import-xml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ Bun natively supports `.xml` imports.

Import the file like any other source file. The module is the parsed document: one key for the root element, `"@name"` keys for attributes, and arrays for repeated elements. Every value is a string.

TypeScript types the module as `Bun.XML.Document`, which describes any XML document. Before you read from the module, give it the shape of your file. A child element that occurs once is a single value, not an array. See [One or many](/runtime/xml#one-or-many).
Comment thread
robobun marked this conversation as resolved.
Resolved

```ts config.ts icon="/icons/typescript.svg"
import doc from "./config.xml";

doc.config["@env"]; // => "production"
doc.config.database["@host"]; // => "localhost"
doc.config.server["@port"]; // => "3000"
doc.config.feature.map(f => f["@name"]); // => ["auth", "rateLimit"]
const { config } = doc as {
config: {
"@env": string;
database: { "@host": string; "@port": string; "@name": string };
server: { "@port": string; "@timeout": string };
feature: { "@name": string }[];
};
};

config["@env"]; // => "production"
config.database["@host"]; // => "localhost"
config.server["@port"]; // => "3000"
config.feature.map(f => f["@name"]); // => ["auth", "rateLimit"]
```

---
Expand All @@ -42,20 +53,20 @@ console.log(Number(config.server["@timeout"])); // => 30

---

For parsing XML strings at runtime, use `Bun.XML.parse()`:
For parsing XML strings at runtime, use `Bun.XML.parse()`. The result is typed as a generic document, so give it the shape you expect:

```ts config.ts icon="/icons/typescript.svg"
const data = Bun.XML.parse(`
const { user } = Bun.XML.parse(`
<user id="7">
<name>John Doe</name>
<hobby>reading</hobby>
<hobby>coding</hobby>
</user>
`);
`) as { user: { "@id": string; name: string; hobby: string[] } };

console.log(data.user.name); // => "John Doe"
console.log(data.user.hobby); // => ["reading", "coding"]
console.log(data.user["@id"]); // => "7"
console.log(user.name); // => "John Doe"
console.log(user.hobby); // => ["reading", "coding"]
console.log(user["@id"]); // => "7"
```

---
Expand Down
2 changes: 1 addition & 1 deletion docs/runtime/file-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ XML files can be directly imported. Bun parses them with its native XML 1.0 pars

```ts
import doc from "./config.xml";
console.log(doc.config["@version"]);
console.log(doc);

// via import attribute:
import feed from "./export.rss" with { type: "xml" };
Expand Down
12 changes: 9 additions & 3 deletions docs/runtime/xml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,18 @@ You can import XML files directly. Bun decodes the file like bytes passed to `XM

#### Default Import

TypeScript types the default import as `XML.Document`, which describes any XML document. Before you read from the import, give it the shape you expect. Write the shape as a type literal or a `type` alias: TypeScript reports `TS2352` for the same shape written as an `interface`. A child element that occurs once is a single value, not an array. See [One or many](#one-or-many).

```ts app.ts icon="/icons/typescript.svg"
import doc from "./config.xml";

console.log(doc.config["@env"]); // "production"
console.log(doc.config.database["@host"]); // "localhost"
console.log(doc.config.feature.map(f => f["@name"])); // ["auth", "rateLimit"]
const { config } = doc as {
config: { "@env": string; database: { "@host": string }; feature: { "@name": string }[] };
};

console.log(config["@env"]); // "production"
console.log(config.database["@host"]); // "localhost"
console.log(config.feature.map(f => f["@name"])); // ["auth", "rateLimit"]
```

#### Named Import
Expand Down
Loading