3

好的,我今天的问题是我试图从我的 .NET Web 服务返回一个 XML 结果。XML 是直接从 SQL 存储过程生成的,XML 如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
 <Details>
  <KlockleId>US0065NOVA</KlockleId> 
  <ListingPrice>54535.612830620680</ListingPrice> 
  <CurrencyCountry>US</CurrencyCountry> 
  <RealStateTax>0.000000000000</RealStateTax> 
  <CommonCharge>0.000000000000</CommonCharge> 
  <SquareFeet>4.200000000000000e+002</SquareFeet> 
  <ActivationDate>2011-04-19T16:16:46.577</ActivationDate> 
  <LotSize>0.000000000000000e+000</LotSize> 
  <YearBuilt>1969</YearBuilt> 
  <FirstName>Salmon Realty</FirstName> 
  <Phone>386 5474221</Phone> 
  <PropertyType>Condo/Co-op</PropertyType> 
  <Construction>Concrete</Construction> 
  <TotalRooms>None</TotalRooms> 
  <Bedroom>None</Bedroom> 
  <BathRoom>One</BathRoom> 
  <HalfBath>None</HalfBath> 
  <Address>600 Atlantic Ave N Unit: 1117</Address> 
  <Area>Volusia</Area> 
  <City>Daytona Beach</City> 
  <Country>United States</Country> 
  <PostalCode>32118</PostalCode> 
  <State>Florida</State> 
  <GMTOffset>0</GMTOffset> 
  <Status>Active</Status> 
  <Zooning>Residential</Zooning> 
  <PublicProperty>1</PublicProperty> 
  <DateAdded>2011-04-19T16:17:00</DateAdded> 
  <PropertyCategory>1</PropertyCategory> 
  <LoadType>2</LoadType> 
 </Details>

现在,当我使用 XML Pull Parser 时,它出现了一个错误:

05-12 21:42:02.963: WARN/System.err(1060): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@44e847a8) 


我的 XML 解析器代码如下所示:

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;

public class PropertyDetailsHandler {

    private ArrayList<PropertyDetails> details;
    private StringReader xmlReader;

    private final String DETAILS = "Details";
    private final String COUNTRY = "Country";
    private final String CITY = "City";

    public PropertyDetailsHandler(String xml) {
        xmlReader = new StringReader(xml);
    }

    public void parse() throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(xmlReader);

        // The StockQuote that is currently being parsed
        PropertyDetails currentProperty = null;

        // The current event returned by the parser
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String xmlNodeName;

            switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                details = new ArrayList<PropertyDetails>();
                break;
            case XmlPullParser.START_TAG:
                xmlNodeName = parser.getName();
                if (xmlNodeName.equalsIgnoreCase(DETAILS)) {
                    // When the  element is reached, create a new
                    // StockQuote.
                    currentProperty = new PropertyDetails();
                } else if (xmlNodeName.equalsIgnoreCase(COUNTRY)) {
                    currentProperty.setCountry(parser.nextText());
                } else if (xmlNodeName.equalsIgnoreCase(CITY)) {
                    currentProperty.setCity(parser.nextText());
                }
                break;
            case XmlPullParser.END_TAG:
                xmlNodeName = parser.getName();
                if (xmlNodeName.equalsIgnoreCase(DETAILS)) {
                    details.add(currentProperty);
                    break;
                }
            }

            eventType = parser.next();
        }
    }

    public ArrayList<PropertyDetails> getCountry() {
        return details;
    }
}

现在我只是试图从 XML 中解析城市国家,但正如您从错误中看到的那样,它甚至根本没有收到我的标签。那么,有什么建议吗?

4

1 回答 1

0

听起来您的 XMLReader 实例是空的:没有拉入任何内容。您检索密码的 URL 是否以某种方式进行了身份验证?

05-12 21:42:02.963: WARN/System.err(1060): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@44e847a8) 

注意位置声明:1:0表示错误发生在第 0 行的字符 1(或第 1 行的字符 0,我永远记不清它们是如何从一个版本到另一个版本计算这些东西的)。

于 2011-05-13T14:01:33.713 回答