diff options
| author | ruki <[email protected]> | 2025-11-15 19:44:28 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-11-15 19:44:28 +0800 |
| commit | 48159a7a047aabcd77a4edb4cc98b6ecf3c51039 (patch) | |
| tree | 46ed30700c079ede4e9174b7a3066c1e37ea1544 | |
| parent | 0a21a3d88e6126b5bb691d393bfc7499810e3d4a (diff) | |
update comment
| -rw-r--r-- | xmake/core/base/xml.lua | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/xmake/core/base/xml.lua b/xmake/core/base/xml.lua index ab04c9696..25c43423d 100644 --- a/xmake/core/base/xml.lua +++ b/xmake/core/base/xml.lua @@ -36,13 +36,27 @@ local table = require("base/table") -- prolog = { comment/doctypes before root } or nil (only on root element) -- } -- --- Example: --- local doc = xml.decode("<root id='1'><item>foo</item><!--note--></root>") --- local node = xml.find(doc, "root/item") --- node.attrs = {lang = "en"} --- node.children = {xml.text("bar")} --- xml.encode(doc) == '<root id="1"><item lang="en">bar</item><!--note--></root>' --- -- nodes are regular Lua tables, so modifying them in-place updates the DOM +-- Quick start: +-- local doc = assert(xml.decode([[ +-- <?xml version="1.0"?><root id="1"><item id="foo">hello</item></root> +-- ]])) +-- local item = assert(xml.find(doc, "//item[@id='foo']")) +-- item.attrs.lang = "en" -- mutate attributes directly +-- item.children = {xml.text("world")} -- replace children with new nodes +-- table.insert(doc.children, xml.comment("generated by xmake")) +-- table.insert(doc.children, xml.new({name = "extra", children = {xml.text("42")}})) +-- local pretty = assert(xml.encode(doc, {pretty = true})) +-- -- save or reuse `pretty` +-- +-- Streaming example (no full DOM in memory): +-- xml.scan(xml_text, function(node) +-- if node.name == "item" then +-- print("item payload:", xml.text_of(node)) +-- if xml.text_of(node) == "stop" then +-- return false -- early terminate +-- end +-- end +-- end) -- -- decode entities |
