-1

这是我要解析的 XML:

val response = "<?xml version=\"1.0\" encoding=\"utf-8\"?><createCustomerProfileResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\"><messages><resultCode>Error</resultCode><message><code>E00039</code><text>A duplicate record with ID 8386840 already exists.</text></message></messages><customerPaymentProfileIdList /><customerShippingAddressIdList /><validationDirectResponseList><string>1,1,1,This transaction has been approved.,0GR6L8,Y,2173844354,none,Test transaction for ValidateCustomerPaymentProfile.,0.00,CC,auth_only,,Test21,Rasmussen,,1234 test st,San Diego,CA,92107,,,,,none,none,none,none,none,none,none,none,0.00,0.00,0.00,FALSE,none,163C552C4DDDC440BAA35C76C47EB79D,,2,,,,,,,,,,,XXXX0027,Visa,,,,,,,,,,,,,,,,</string></validationDirectResponseList></createCustomerProfileResponse>"

这是我的代码:

val source = Source.fromString(response)
val html = new HTML5Parser
val htmlObject = html.loadXML(source)

var resultCode = (htmlObject \ "createCustomerProfileResponse" \ "messages" \ "resultCode").text

resultCode 始终为空。我究竟做错了什么?

编辑:

我更新了响应 XML。现在它只有一个根 XML。它仍然不起作用。

我还将 html5Parser 从 models.html5Parser 更改为 html5Parser。它仍然不起作用。

4

1 回答 1

1

你的结构的根已经是隐式的(createCustomerProfileResponse)所以你不需要选择它。

这应该有效:

var resultCode = (htmlObject \ "messages" \ "resultCode").text
于 2012-07-24T12:25:23.770 回答