Lua XML从模式中提取 [英] Lua XML extract from pattern

查看:156
本文介绍了Lua XML从模式中提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个应用程序正在向我的脚本发送像这样的流:

An application is sending my script an Stream like this one:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <aRootChildNode>
    <anotherChildNode>
     <?xml version="1.0">
     <TheNodeImLookingFor>
       ... content ...
     </TheNodeImLookingFor>
    </anotherChildNode>
   </aRootChildNode>
</root>

我想提取TheNodeImLookingFor部分. 到目前为止,得到了:

I want to extract the TheNodeImLookingFor section. So far, got:

data = string.match(Stream, "^.+\<TheNodeImLookingFor\>.+\<\/TheNodeImLookingFor\>.+$")

模式在流中可以识别,但不会提取节点及其内容.

Pattern is recognized in the Stream, but it doesn't extract the node and its content.

推荐答案

通常,使用模式匹配(Lua模式或正则表达式)提取XML并不是一个好主意.使用XML解析器.

In general, it's not a good idea to use pattern matching (either Lua pattern or regex) to extract XML. Use a XML parser.

对于此问题,您无需转义\<(即使这样做,Lua模式也会使用%转义魔术字符).并使用方括号获取节点及其内容:

For this problem, you don't need to escape \ or <(even if you do, Lua pattern uses % to escape magic characters). And use brackets to get the node and its content:

data = string.match(Stream, "^.+(<TheNodeImLookingFor>.+</TheNodeImLookingFor>).+$")

或者仅获取内容:

data = string.match(Stream, "^.+<TheNodeImLookingFor>(.+)</TheNodeImLookingFor>.+$")

这篇关于Lua XML从模式中提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆