如何检查XML文件是否存在节点并在Progress4GL中检索值

如何检查XML文件是否存在节点并在Progress4GL中检索值,xml,openedge,progress-4gl,Xml,Openedge,Progress 4gl,您好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/Tra

您好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>
 ...
现在我想检查我的响应是否有跟踪号,如果有,我想将跟踪号存储为变量。这似乎是可能的:

然而,这是我到目前为止所做的,它似乎不起作用。没有输出任何内容,但没有运行时错误:

....
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.
....

您需要沿着树向下走才能到达所需的节点。像下面这样的工作。这是一个劳累的地方,你需要自己清理一下(就像最后一个街区一样)


您需要沿着树向下走才能到达所需的节点。像下面这样的工作。这是一个劳累的地方,你需要自己清理一下(就像最后一个街区一样)


虽然Peter的答案没有错,但使用自动XML到数据集映射的功能可以简单得多。在这种情况下,您甚至不需要将数据集静态建模为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.
def var lcresponse as longchar initial'
1.
成功
01
船舶识别号
MYTRACKERNUMBER
'.
def var hds作为句柄不可撤消。
def var hb作为句柄不可撤消。
创建数据集hds。
hds:读取xml(“longchar”、lcresponse、、、、、、?)。
hb=hds:get缓冲区句柄(“InquiryNumber”)。
hb:find-unique()无错误。
如果hb:可用,则
消息hb:缓冲区字段(“值”):缓冲区值。

虽然Peter的答案没有错,但使用自动XML到数据集映射的功能可以简单得多。在这种情况下,您甚至不需要将数据集静态建模为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.
def var lcresponse as longchar initial'
1.
成功
01
船舶识别号
MYTRACKERNUMBER
'.
def var hds作为句柄不可撤消。
def var hb作为句柄不可撤消。
创建数据集hds。
hds:读取xml(“longchar”、lcresponse、、、、、、?)。
hb=hds:get缓冲区句柄(“InquiryNumber”)。
hb:find-unique()无错误。
如果hb:可用,则
消息hb:缓冲区字段(“值”):缓冲区值。

谢谢Stefan,我想知道这是怎么可能的,因为数据位于一个名为“value”的节点中,这是一个静态临时表字段的关键字,但这是一个解决方案,工作非常出色!再次感谢!是的,保留关键字可能会造成麻烦,在这种情况下,它会阻止::快捷方式表单(消息hb::Value)。对于静态临时表,您通常可以通过使用serialize name(so:field myvalue serialize name“value”)来解决这个问题。感谢Stefan,我想知道这是怎么可能的,因为数据位于名为“value”的节点中,这是静态临时表字段的关键字,但这是一个解决方案,工作非常出色!再次感谢!是的,保留关键字可能会造成麻烦,在这种情况下,它会阻止::快捷方式表单(消息hb::Value)。对于静态临时表,您通常可以通过使用serialize name(so:field myvalue serialize name“value”)来解决此问题。感谢@nwahmaet,这对于理解如何使用x-noderef句柄获取XML文件中所需的内容非常有帮助。:)感谢@nwahmaet,这对于理解如何使用x-noderef句柄获取XML文件中所需的内容非常有帮助。:)
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.