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] = "jy@163.com"
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>jy@163.com</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>jy@163.com</email>
  <phone>
    <number>158</number>
  </phone>
</student>


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值