Warm tip: This article is reproduced from serverfault.com, please click

sql-冷融

(sql - Coldfusion)

发布于 2020-12-17 02:14:05

我正在尝试获取两个字符串之间的特定数据,这两个字符串是一个开始和结束标记。通常我只是使用XmlParse来解析它,但是问题在于它在数据集中还有很多其他垃圾。

这是大字符串的示例:

test of data need to parse:<?xml version="1.0" encoding="UTF-8"?><alert xmlns="urn:oasis:names:tc::cap:1.2"><identifier>_2020-12-16T17:32:5620201116173256</identifier><sender>683</sender><sent>2020-12-16T17:32:56-05:00</sent><status>Test</status><msgType>Alert</msgType><source>test of data need to parse</source><scope>Public</scope><addresses/><code>Test1.0</code><note>WENS IPAWS</note><info><language>en-US</language></info>


<capsig:Signature xmlns:capsig="http://www.w3.org/2000/09/xmldsig">

<capsig:Info>
<capsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n"/>
<capsig:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-morersa-sha256"/>
<capsig:Referrer URI="">
<capsig:Trans>
<capsig:Trans Algorithm="http://www.w3.org/2000/09/xmldsigenveloped-signature"/>
</capsig:Trans>
<capsig:DMethod Algorithm="http://www.w3.org/2001/04/xmlencsha256"/>
<capsig:DigestValue>wjL4tqltJY7m/4=</capsig:DigestValue>
</capsig:Referrer>
</capsig:Info>


test of data need to parse:<?xml version="1.0" encoding="UTF-8"?><alert xmlns="urn:oasis:names:tc::cap:1.2"><identifier>_2020-12-16T17:32:5620201116173256</identifier><sender>683</sender><sent>2020-12-16T17:32:56-05:00</sent><status>Test</status><msgType>Alert</msgType><source>test of data need to parse</source><scope>Public</scope><addresses/><code>Test1.0</code><note>WENS IPAWS</note><info><language>en-US</language></info>

因此,我需要做的只是提取以下内容:

 <capsig:Info>
 <capsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n"/>
 <capsig:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-morersa-sha256"/>
 <capsig:Referrer URL="">
 <capsig:Trans>
 <capsig:Trans Algorithm="http://www.w3.org/2000/09/xmldsigenveloped-signature"/>
 </capsig:Trans>
 <capsig:DMethod Algorithm="http://www.w3.org/2001/04/xmlencsha256"/>
 <capsig:DigestValue>wjL4tqltJY7m/4=</capsig:DigestValue>
 </capsig:Referrer>
 </capsig:Info>

我到处搜索,发现可以用字符和计数完成操作,但没有一个真正起作用。尝试使用SQL进行此操作,但是因为字符串中的不断变化会导致问题。所以我的计划是获取“ capsig:Info”之后和“ </ capsig:Info>”之前的所有内容,然后将其插入表中。

有办法用Coldfusion做到这一点吗?

任何建议,将不胜感激。

谢谢!

Questioner
Scott
Viewed
0
user12031119 2020-12-17 22:29:05

是的,你可以<capsig:Info> ... </capsig:Info>通过使用ColdFusion函数使用正则表达式匹配来提取包含标签之间文本的子字符串,该函数reMatch()将返回与指定模式匹配的所有子字符串的数组。可以使用下面的代码行来完成。

<!--- Use reMatch to extract all pattern matches into an array --->
<cfset parsedXml = reMatch("<capsig:Info>(.*?)</capsig:Info>", xmlToParse)>

<!--- parsedXml is an array of strings.  The result will be found in the first array element as such --->
<cfdump var="#parsedXml[1]#" label="parsedXml">

你可以在此处使用演示查看。

https://trycf.com/gist/00be732d93ef49b2427768e18e371527/lucee5?theme=monokai