2012-07-23 106 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