const { XMLParser } = import.meta.use('xml');const parser = new XMLParser();parser .on("startElement", (name, attrs) => { console.log(`<${name}>`, attrs); }) .on("endElement", (name) => { console.log(``); }) .on("characterData", (data) => { const trimmed = data.trim(); if (trimmed) console.log(`Text: ${trimmed}`); });const xml1 = ` <root> <item id="1">Hello World</item> <item id="2">txiki.js</item> </root>`;parser.parse(xml1); Copy
const { XMLParser } = import.meta.use('xml');const parser = new XMLParser();parser .on("startElement", (name, attrs) => { console.log(`<${name}>`, attrs); }) .on("endElement", (name) => { console.log(``); }) .on("characterData", (data) => { const trimmed = data.trim(); if (trimmed) console.log(`Text: ${trimmed}`); });const xml1 = ` <root> <item id="1">Hello World</item> <item id="2">txiki.js</item> </root>`;parser.parse(xml1);
const parser2 = new XMLParser({ namespace: true, namespaceSeparator: ":"});parser2 .on("startNamespace", (prefix, uri) => { console.log(`xmlns${prefix ? `:${prefix}` : ""} = "${uri}"`); }) .on("startElement", (name, attrs) => { console.log(`Element: ${name}`, attrs); });const xml2 = ` <root xmlns="http://example.com/ns" xmlns:foo="http://foo.com"> <foo:item>Content</foo:item> </root>`;parser2.parse(xml2); Copy
const parser2 = new XMLParser({ namespace: true, namespaceSeparator: ":"});parser2 .on("startNamespace", (prefix, uri) => { console.log(`xmlns${prefix ? `:${prefix}` : ""} = "${uri}"`); }) .on("startElement", (name, attrs) => { console.log(`Element: ${name}`, attrs); });const xml2 = ` <root xmlns="http://example.com/ns" xmlns:foo="http://foo.com"> <foo:item>Content</foo:item> </root>`;parser2.parse(xml2);
const parser3 = new XMLParser();const elements: string[] = [];parser3 .on("startElement", (name) => { elements.push(name); }) .on("endElement", () => { elements.pop(); });try { // 分块解析 parser3.parse("<root><item>", false); parser3.parse("Hello</item>", false); parser3.parse("</root>", true); console.log("解析成功!");} catch (err) { console.error(`解析错误在第 ${parser3.line} 行, 第 ${parser3.column} 列:`, err);} Copy
const parser3 = new XMLParser();const elements: string[] = [];parser3 .on("startElement", (name) => { elements.push(name); }) .on("endElement", () => { elements.pop(); });try { // 分块解析 parser3.parse("<root><item>", false); parser3.parse("Hello</item>", false); parser3.parse("</root>", true); console.log("解析成功!");} catch (err) { console.error(`解析错误在第 ${parser3.line} 行, 第 ${parser3.column} 列:`, err);}
const { XMLParser } = import.meta.use('xml');const parser4 = new XMLParser();parser4 .on("comment", (data) => { console.log(`Comment: ${data}`); }) .on("startCDATA", () => { console.log("CDATA 开始"); }) .on("characterData", (data) => { console.log(`数据: ${data}`); }) .on("endCDATA", () => { console.log("CDATA 结束"); });const xml4 = ` <root> <!-- 这是一个注释 --> <!--[CDATA[<Special--> & "Characters"]]> </root>`;parser4.parse(xml4); Copy
const { XMLParser } = import.meta.use('xml');const parser4 = new XMLParser();parser4 .on("comment", (data) => { console.log(`Comment: ${data}`); }) .on("startCDATA", () => { console.log("CDATA 开始"); }) .on("characterData", (data) => { console.log(`数据: ${data}`); }) .on("endCDATA", () => { console.log("CDATA 结束"); });const xml4 = ` <root> <!-- 这是一个注释 --> <!--[CDATA[<Special--> & "Characters"]]> </root>`;parser4.parse(xml4);
const { XMLParser } = import.meta.use('xml');const parser5 = new XMLParser();parser5.on("processingInstruction", (target, data) => { console.log(`PI: `);});const xml5 = `<root>`;parser5.parse(xml5); Copy
const { XMLParser } = import.meta.use('xml');const parser5 = new XMLParser();parser5.on("processingInstruction", (target, data) => { console.log(`PI: `);});const xml5 = `<root>`;parser5.parse(xml5);
import { escape } from "@tjs/xml";const userInput = '<script>alert("XSS")</script>';const safeXML = `<content>${escape(userInput)}</content>`;console.log(safeXML);// 输出: <content><script>alert("XSS")</script></content>// Example 7: 构建类似DOM的结构import { XMLParser, XMLAttributes } from "@tjs/xml";interface XMLNode { name: string; attrs: XMLAttributes; children: (XMLNode | string)[];}function parseToTree(xml: string): XMLNode | null { const parser = new XMLParser(); const stack: XMLNode[] = []; let root: XMLNode | null = null; parser .on("startElement", (name, attrs) => { const node: XMLNode = { name, attrs, children: [] }; if (stack.length > 0) { stack[stack.length - 1].children.push(node); } else { root = node; } stack.push(node); }) .on("endElement", () => { stack.pop(); }) .on("characterData", (data) => { const trimmed = data.trim(); if (trimmed && stack.length > 0) { stack[stack.length - 1].children.push(trimmed); } }); parser.parse(xml); return root;}const tree = parseToTree("<root><item id="1">Hello</item></root>");console.log(JSON.stringify(tree, null, 2)); Copy
import { escape } from "@tjs/xml";const userInput = '<script>alert("XSS")</script>';const safeXML = `<content>${escape(userInput)}</content>`;console.log(safeXML);// 输出: <content><script>alert("XSS")</script></content>// Example 7: 构建类似DOM的结构import { XMLParser, XMLAttributes } from "@tjs/xml";interface XMLNode { name: string; attrs: XMLAttributes; children: (XMLNode | string)[];}function parseToTree(xml: string): XMLNode | null { const parser = new XMLParser(); const stack: XMLNode[] = []; let root: XMLNode | null = null; parser .on("startElement", (name, attrs) => { const node: XMLNode = { name, attrs, children: [] }; if (stack.length > 0) { stack[stack.length - 1].children.push(node); } else { root = node; } stack.push(node); }) .on("endElement", () => { stack.pop(); }) .on("characterData", (data) => { const trimmed = data.trim(); if (trimmed && stack.length > 0) { stack[stack.length - 1].children.push(trimmed); } }); parser.parse(xml); return root;}const tree = parseToTree("<root><item id="1">Hello</item></root>");console.log(JSON.stringify(tree, null, 2));
Example: Example 1: 基本XML解析
Example: Example 2: 命名空间感知解析
Example: Example 3: 流式解析及错误处理
Example: Example 4: 构建XML字符串
Example: Example 5: 构建XML字符串
Example: Example 6: 转义XML内容