【发布时间】:2019-09-20 02:36:31
【问题描述】:
我正在尝试解析来自在线支付网关处理器的 XML 网关响应。然后利用这些变量来更新各种联系、机会和交易记录。 我正在努力从 XML 中获取值
我只是不太了解 XML Parseing,所以我试图先获取我需要返回的值,然后我可以担心从 @invocablemethod 重试它们。
String trans = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><DirectPaymentResponse xmlns="https://api.ewaypaymentscom/"><DirectPaymentResult><AuthorisationCode>0000C</AuthorisationCode><ResponseCode>00</ResponseCode><ResponseMessage>A2000</ResponseMessage><TransactionID>0000003</TransactionID><TransactionStatus>true</TransactionStatus><TransactionType>MOTO</TransactionType><BeagleScore>0.6</BeagleScore><Verification><CVN>Unchecked</CVN><Address>Unchecked</Address><Email>Unchecked</Email><Mobile>Unchecked</Mobile><Phone>Unchecked</Phone></Verification><Customer><TokenCustomerID>66666666</TokenCustomerID><Reference>Reference</Reference><Title>Mr.</Title><FirstName>aan</FirstName><LastName>McDat</LastName><CompanyName>Perkins</CompanyName><JobDescription>Officer</JobDescription><Street1>23 Verdun Street</Street1><Street2 /><City>Nedlands</City><State>WA</State><PostalCode>6009</PostalCode><Country>au</Country><Email>t@perkins.org.au</Email><Phone>0811111111</Phone><Mobile>0422222222</Mobile><Comments>Hi there</Comments><Fax /><Url>perkins.org.au</Url><CardDetails><Number>452012XXXXX9</Number><Name>ASd estt</Name><ExpiryMonth>1</ExpiryMonth><ExpiryYear>19</ExpiryYear><StartMonth /><StartYear /><IssueNumber /><CVN /></CardDetails></Customer><Payment><TotalAmount>200</TotalAmount><InvoiceNumber>0067F00000UmnAJ</InvoiceNumber><InvoiceDescription /><InvoiceReference /><CurrencyCode>AUD</CurrencyCode></Payment></DirectPaymentResult></DirectPaymentResponse></soap:Body></soap:Envelope>';
上面是我收到的字符串
Dom.Document doc = new Dom.Document();
Doc.load(trans);
然后我向下处理节点以尝试获取值。
Dom.XmlNode Env = doc.getRootElement();
system.debug('Root Element'+doc.getRootElement());
Dom.XmlNode body = Env.getChildElement('Body', 'http://www.w3.org/2003/05/soap-envelope');
system.debug('Child Element'+Env.getChildElement('Body', 'http://www.w3.org/2003/05/soap-envelope'));
List<Dom.XmlNode> cElements = Env.getChildElements();
system.debug('Child Elements'+Env.getChildElements());
Dom.XmlNode DirectPR = body.getChildElement('DirectPaymentResponse','https://api.ewaypaymentscom/');
system.debug('DirectPR'+body.getChildElement('DirectPaymentResponse','https://api.ewaypaymentscom/'));
Dom.XmlNode DirectPResult = DirectPR.getChildElement('DirectPaymentResult','https://api.ewaypaymentscom/');
system.debug('Direct Payment Results'+DirectPR.getChildElement('DirectPaymentResult','https://api.ewaypaymentscom/'));
Dom.XmlNode AuthCodes = DirectPResult.getChildElement('AuthorisationCode', 'https://api.ewaypaymentscom/');
system.debug('Auth Element '+DirectPResult.getChildElement('AuthorisationCode', 'https://api.ewaypaymentscom/'));
Dom.XmlNode AUText = AuthCodes.getChildElement('Text', 'https://api.ewaypaymentscom/');
system.debug('Auth Element '+AuthCodes.getChildElement('Text', 'https://api.ewaypaymentscom/'));
String Conde = AuthCodes.getAttributeValue('AuthorisationCode', 'https://api.ewaypaymentscom/');
system.debug('Auth Code; '+AuthCodes.getAttributeValue('AuthorisationCode', 'https://api.ewaypaymentscom/'));`
我尝试返回值,但不断收到“发生错误:发生 Apex 错误:System.NullPointerException:尝试取消引用空对象” 并且在某一时刻也得到了 'System.XmlException: 在开始标记之前只允许空白内容,而不是 {' 但无法复制该问题。 请注意,我已经从“api.ewaypaymentscom”中删除了这些点,因为它只允许我包含 10 个。如果我从 getChildElement 方法中删除它们,它只会返回 null。 目前我正在使用执行匿名窗口来测试代码,但想在可调用的方法类中使用它。
【问题讨论】:
标签: xml parsing xml-parsing salesforce apex