2012-07-23 107 views
-1

这里是我试图解析XML: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。它仍然不起作用。

+0

不知道XML是有效的,是吗?如果您检查它,它有2个根处理指令,后跟根元素。它看起来像2个文档连接。 – 2012-07-23 17:06:14

+0

'error:not found:value models' - 你需要澄清你暗示的是哪个库('HTML5Parser') – 2012-07-23 17:06:30

+0

什么是'HTML5Parser'? – 2012-07-23 18:47:15

回答

1

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

这应该工作:

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