Lua XML extract from pattern

一个人想着一个人 提交于 2019-12-21 21:27:56

问题


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>

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.


回答1:


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

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>).+$")

Or to get only the content:

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


来源:https://stackoverflow.com/questions/23558806/lua-xml-extract-from-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!