如何检查XML文件是否存在节点并在Progress4GL中检索值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检查XML文件是否存在节点并在Progress4GL中检索值相关的知识,希望对你有一定的参考价值。
Hello Progress4GL开发人员,
在成功对UPS进行SOAP调用后,我将以下XML响应存储在名为cBody的longchar变量中:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<trk:TrackResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:trk="http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0"><common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
<common:TransactionReference/>
</common:Response>
<trk:Shipment>
<trk:InquiryNumber>
<trk:Code>01</trk:Code>
<trk:Description>ShipmentIdentificationNumber</trk:Description>
<trk:Value>MYTRACKERNUMBER</trk:Value>
</trk:InquiryNumber>
...
我现在使用以下代码将其存储为X-DOCUMENT:
hDoc:LOAD("longchar",cBody2,FALSE).
现在,我想检查我的回复中是否有跟踪号,如果有,我想将跟踪号存储为变量。看来这是可能的:https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/dvxml/examples-of-reading-an-input-xml-file.html
不过这是我到目前为止所拥有的,而且似乎没有用。什么也没有输出,但是没有运行时错误:
....
hDoc:LOAD("longchar",cBody,FALSE).
DEFINE variable hNodeRef as HANDLE NO-UNDO.
CREATE X-NODEREF hNodeRef.
hDoc:GET-DOCUMENT-ELEMENT(hNodeRef).
IF hNodeRef:NAME = "trk:value" THEN
message hNoderef:GET-ATTRIBUTE("id") hNoderef:ATTRIBUTE-NAMES.
....
答案
您需要沿着树走到想要的节点。类似于下面的作品。这很费力,您需要自己进行清理(如在finally块中一样)。
define variable hXmlDoc as handle no-undo.
define variable hXmlNode as handle no-undo.
define variable hXmlNode2 as handle no-undo.
define variable hXmlNode3 as handle no-undo.
define variable loop as integer no-undo.
define variable cnt as integer no-undo.
create x-document hXmlDoc.
hXmlDoc:load('file', 'soap.xml', no).
create x-noderef hXmlNode.
create x-noderef hXmlNode2.
create x-noderef hXmlNode3.
hXmlDoc:get-document-element(hXmlNode).
cnt = hXmlNode:num-children.
do loop = 1 to cnt:
hXmlNode:get-child(hXmlNode2, loop).
if hXmlNode2:name eq 'trk:Shipment' then
leave.
end.
cnt = hXmlNode2:num-children.
do loop = 1 to cnt:
hXmlNode2:get-child(hXmlNode3, loop).
if hXmlNode3:name eq 'trk:InquiryNumber' then
leave.
end.
// should be trk:InquiryNumber
cnt = hXmlNode3:num-children.
do loop = 1 to cnt:
hXmlNode3:get-child(hXmlNode, loop).
if hXmlNode:name eq 'trk:Value' then
leave.
end.
// should be trk:Value
hXmlNode:get-child(hXmlNode2, 1).
message
hXmlNode:name skip
hXmlNode2:node-value
view-as alert-box.
finally:
delete object hXmlNode.
delete object hXmlNode2.
delete object hXmlNode3.
delete object hXmlDoc.
end finally.
另一答案
尽管Peter的回答没有错,但是使用自动XML到DataSet映射的功能可能要简单得多。在这种情况下,您甚至不需要将数据集静态地建模为XML,ABL就会为您自动完成:
def var lcresponse as longchar initial '<?xml version="1.0" encoding="ISO-8859-1" ?>
<trk:TrackResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:trk="http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0"><common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
<common:TransactionReference/>
</common:Response>
<trk:Shipment>
<trk:InquiryNumber>
<trk:Code>01</trk:Code>
<trk:Description>ShipmentIdentificationNumber</trk:Description>
<trk:Value>MYTRACKERNUMBER</trk:Value>
</trk:InquiryNumber>
</trk:Shipment>
</trk:TrackResponse>'.
def var hds as handle no-undo.
def var hb as handle no-undo.
create dataset hds.
hds:read-xml( "longchar", lcresponse, ?, ?, ? ).
hb = hds:get-buffer-handle("InquiryNumber").
hb:find-unique() no-error.
if hb:available then
message hb:buffer-field("Value"):buffer-value.
https://abldojo.services.progress.com:443/#/?shareId=5ed946344b1a0f40c34b8c5a
以上是关于如何检查XML文件是否存在节点并在Progress4GL中检索值的主要内容,如果未能解决你的问题,请参考以下文章
如何使用节点 js 检查“s3”/“azure”路径中是不是存在文件