lua 解析 xml 文件,含内嵌

-- 导入依赖文件
require('LuaXml')
local socket = require "socket"
-- 加载XML文件
local xfile = xml.load("student.xml")
-- 添加子节点
local xNewFile = xml.new("student")

local start_time = socket.gettime()
for i=1, 1000 do
	-- 查找子节点
	local id = xfile:find("id")
	local name = xfile:find("name")
	local cla = xfile:find("cla")
	local school = xfile:find("school")
	local college = xfile:find("college")
	local email = xfile:find("email")
	local phone = xfile:find("phone")

end

-- 设置子节点键值
xNewFile:append("id")[1] = 10
xNewFile:append("name")[1] = "LUA"
xNewFile:append("cla")[1] = "wang"
xNewFile:append("school")[1] = "zh"
xNewFile:append("college")[1] = "xin"
xNewFile:append("email")[1] = "[email protected]"
phone = xNewFile:append("phone")
phone:append("number")[1] = "158"

print( xNewFile);
-- 保存文件
xNewFile:save"student_new.xml";

local end_time = socket.gettime()

print(end_time-start_time)


student.xml文件内容如下:

<student>  
    <id>10</id>  
    <name>LUA</name>
    <cla>wang</cla>
    <school>zh</school> 
    <college>xin</college>
    <email>[email protected]</email>
    <phone num="1">
        <field number="158">
</student> 

得到的student_new.xml文件:

<?xml version="1.0"?>
<!-- file "student_new.xml", generated by LuaXML -->

<student>
  <id>10</id>
  <name>LUA</name>
  <cla>wang1</cla>
  <school>zh</school>
  <college>xin</college>
  <email>[email protected]</email>
  <phone>
    <number>158</number>
  </phone>
</student>


你可能感兴趣的:(xml,lua)