2014-10-28 141 views
1

我想解析一個java文件中的XML文件,但我面臨一個問題。該代碼在解析過程中顯示錯誤,就好像它正在獲取JSON文件一樣。我該如何解決這個問題?從url解析XML文件時出現java錯誤

URL url2 = new URL("https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US&currencyCode=USD&longitude=0.1275&latitude=51.5072&xml=%3CHotelListRequest%3E%0A%20%20%20%20%3Ccity%3ESeattle%3C%2Fcity%3E%0A%20%20%20%20%3CstateProvinceCode%3EWA%3C%2FstateProvinceCode%3E%0A%20%20%20%20%3CcountryCode%3EUS%3C%2FcountryCode%3E%0A%20%20%20%20%3CarrivalDate%3E11%2F25%2F2014%3C%2FarrivalDate%3E%0A%20%20%20%20%3CdepartureDate%3E11%2F27%2F2014%3C%2FdepartureDate%3E%0A%20%20%20%20%3CRoomGroup%3E%0A%20%20%20%20%20%20%20%20%3CRoom%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%0A%20%20%20%20%20%20%20%20%3C%2FRoom%3E%0A%20%20%20%20%3C%2FRoomGroup%3E%0A%20%20%20%20%3CnumberOfResults%3E25%3C%2FnumberOfResults%3E%0A%3C%2FHotelListRequest%3E"); 
InputStream is2 = url2.openStream(); 

SAXBuilder builder = new SAXBuilder(); 
Document doc = builder.build(is2); 
StringBuilder sb = new StringBuilder(); 
Element root = doc.getRootElement(); 
List<Element> HotelList = 
     root.getChildren("HotelList"); 
Element Hotel = HotelList.get(0); 
List<Element> SummaryList = 
     Hotel.getChildren("HotelSummary"); 
Element Summary = SummaryList.get(0); 
Element Sum; 
for(int f=0; f< SummaryList.size(); f++){ 
    Sum = SummaryList.get(f); 

    sb.append("Hotel ID: " + Sum.getChildText("hotelId") +"\n" + "Name: " + Sum.getChildText("name") + "\n"); 
    sb.append("HotelRating: " + Sum.getChildText("hotelRating")+ "\n"); 

}System.out.println(sb); 

的錯誤:

org.jdom2.input.JDOMParseException: Error on line 1: Content is not allowed in prolog. 
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232) 
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:253) 
    at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1091) 
    at HotelInfo.main(HotelInfo.java:22) 
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217) 
    ... 3 more 
+0

你看過你想分析的實際XML嗎? XML是如何生成的並不重要,而是與處理解析端時的確切關係。 – thatidiotguy 2014-10-28 18:15:16

回答

1

看看使用curl返回的數據。它是JSON,而不是XML,因此你不能以這種方式解析它。改爲使用JSON解析器,或者找出使服務返回XML所需的API參數。