使用scala解析XML无法正常工作

时间:2012-07-23 17:04:10

标签: xml parsing scala

以下是我要解析的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。它仍然无效。

1 个答案:

答案 0 :(得分:1)

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

这应该有效:

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