2012-10-15 74 views
0

dom4j中毫不費力地做解析response.getEntity(String.class)字符串中的dom4j與DocumentHelper.parseText()爲xml

String text = "<person> <name>James</name> </person>"; 
Document document = DocumentHelper.parseText(text); 

我需要的是這個

String text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 
"<person> <name>James</name> </person>"; 
Document document = DocumentHelper.parseText(text); 

但它拋出一個例外。

org.dom4j.DocumentException: Error on line 1 of document : parsing initialization error: org.gjt.xpp.XmlPullParserException: only whitespace content allowed outside root element at line 1 and column 1 seen 

回答

1

我發現問題所在。下面的前一行是失敗的一行。後續工作。

Document output = DocumentHelper.parseText(response.getEntity(String.class)); 

Document output = DocumentHelper.parseText(response.getEntity(String.class).trim());