org.xml.sax.SAXParseException:意外的标记(位置:java.io.StringReader@4156ccc8中的TEXT @ 1:2)

时间:2014-03-20 21:40:20

标签: android

我的主要活动中有这段代码:

BufferedReader reader = new BufferedReader (new InputStreamReader(in));

        StringBuilder xml1 = new StringBuilder();
        String cursor;
        try {
            while ( (cursor = reader.readLine()) != null){
                xml1.append(cursor);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String xml = xml1.toString();

                XmlParser parser = new XmlParser();
        Document doc = parser.getDomElement(xml); 

这是getDomElement函数:

public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://portal.wow2.telenor.com/global/newscentre/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>032acc78-4645-416d-8f57-69f9de340d9e</id>
  <title />
  <updated>2014-03-20T06:20:12Z</updated>
<entry m:etag="&quot;28&quot;">
    <id>3cd3253c-7a95-4751-b34c-dada815c6d23</id>
    <category term="SP.Data.PagesItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FirstUniqueAncestorSecurableObject" type="application/atom+xml;type=entry" title="FirstUniqueAncestorSecurableObject" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/FirstUniqueAncestorSecurableObject" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RoleAssignments" type="application/atom+xml;type=feed" title="RoleAssignments" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/RoleAssignments" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AttachmentFiles" type="application/atom+xml;type=feed" title="AttachmentFiles" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/AttachmentFiles" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContentType" type="application/atom+xml;type=entry" title="ContentType" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/ContentType" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsHtml" type="application/atom+xml;type=entry" title="FieldValuesAsHtml" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/FieldValuesAsHtml" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsText" type="application/atom+xml;type=entry" title="FieldValuesAsText" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/FieldValuesAsText" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesForEdit" type="application/atom+xml;type=entry" title="FieldValuesForEdit" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/FieldValuesForEdit" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/File" type="application/atom+xml;type=entry" title="File" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/File" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Folder" type="application/atom+xml;type=entry" title="Folder" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/Folder" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ParentList" type="application/atom+xml;type=entry" title="ParentList" href="Web/Lists(guid'b1b97552-232e-46e3-b9ec-f864a85f7177')/Items(2)/ParentList" />
    <title />
    <updated>2014-03-20T06:20:12Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">2</d:Id>
        <d:ContentTypeId>0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D000C4FEEB0930A3E4FAC4CED6DBC3688FA</d:ContentTypeId>
        <d:OData__ModerationComments m:null="true" />
        <d:Title>a1</d:Title>
        <d:OData__dlc_DocId>67XPVZAHCTHM-7-2</d:OData__dlc_DocId>
        <d:OData__dlc_DocIdUrl m:type="SP.FieldUrlValue">
          <d:Description>67XPVZAHCTHM-7-2</d:Description>
          <d:Url>https://portal.wow2.telenor.com/global/newscentre/_layouts/15/DocIdRedir.aspx?ID=67XPVZAHCTHM-7-2</d:Url>
        </d:OData__dlc_DocIdUrl>
        <d:Comments m:null="true" />
        <d:PublishingStartDate m:null="true" />
        <d:PublishingExpirationDate m:null="true" />
        <d:PublishingContactId m:type="Edm.Int32">1</d:PublishingContactId>
        <d:PublishingContactEmail m:null="true" />
        <d:PublishingContactName m:null="true" />
        <d:PublishingContactPicture m:null="true" />
        <d:PublishingPageLayout m:type="SP.FieldUrlValue">
          <d:Description>Body only</d:Description>
          <d:Url>https://portal.wow2.telenor.com/_catalogs/masterpage/PageFromDocLayout.aspx</d:Url>
        </d:PublishingPageLayout>
        <d:PublishingIsFurlPage m:type="Edm.Boolean">true</d:PublishingIsFurlPage>
        <d:SeoBrowserTitle m:null="true" />
        <d:SeoMetaDescription m:null="true" />
        <d:SeoKeywords m:null="true" />
        <d:RobotsNoIndex m:type="Edm.Boolean">false</d:RobotsNoIndex>
        <d:PublishingPageContent>&lt;p&gt;test​&lt;/p&gt;</d:PublishingPageContent>
        <d:ArticleByLine m:null="true" />
        <d:ArticleStartDate m:null="true" />
        <d:PublishingImageCaption m:null="true" />
        <d:TelenorBanner m:null="true" />
        <d:BusinessUnit>
          <d:element m:type="SP.Taxonomy.TaxonomyFieldValue">
            <d:Label>Global</d:Label>
            <d:TermGuid>95e90ed7-544b-4526-8479-b829dbf448f6</d:TermGuid>
            <d:WssId m:type="Edm.Int32">4</d:WssId>
          </d:element>
        </d:BusinessUnit>
        <d:GeoLocation m:null="true" />
        <d:SecurityClassification>Internal</d:SecurityClassification>
        <d:RightColumnContact m:null="true" />
        <d:InformationContentType m:null="true" />
        <d:InformationContextCategory m:null="true" />
        <d:InformationValue>Non-Essential</d:InformationValue>
        <d:TelenorArticleFirstPublishDate m:null="true" />
        <d:ArticleAuthorId m:null="true" />
        <d:ArticleCoAuthorsId m:null="true" />
        <d:Teaser m:null="true" />
        <d:Introduction m:null="true" />
        <d:EnableComments m:type="Edm.Boolean">true</d:EnableComments>
        <d:IsHighlight m:type="Edm.Boolean">false</d:IsHighlight>
        <d:ArticleLanguage m:type="SP.Taxonomy.TaxonomyFieldValue">
          <d:Label>1</d:Label>
          <d:TermGuid>23c0f277-1348-4971-aac7-9bba7d153ecf</d:TermGuid>
          <d:WssId m:type="Edm.Int32">1</d:WssId>
        </d:ArticleLanguage>
        <d:ArticleSummaryLinkText m:null="true" />
        <d:TelenorArticlePublishingDate>2014-02-06T10:28:21Z</d:TelenorArticlePublishingDate>
        <d:EnableCommentsWebArtice m:type="Edm.Boolean">false</d:EnableCommentsWebArtice>
        <d:AggregateTarget />
        <d:Set_x0020_Publish_x0020_Date m:null="true" />
        <d:Set_x0020_Publish_x0020_Date_x0028_1_x0029_ m:null="true" />
        <d:AverageRating m:null="true" />
        <d:RatingCount m:null="true" />
        <d:LikesCount m:null="true" />
        <d:OData__ModerationStatus m:type="Edm.Int32">0</d:OData__ModerationStatus>
        <d:ID m:type="Edm.Int32">2</d:ID>
        <d:Created m:type="Edm.DateTime">2013-07-09T05:59:34Z</d:Created>
        <d:AuthorId m:type="Edm.Int32">1</d:AuthorId>
        <d:Modified m:type="Edm.DateTime">2014-02-06T10:28:21Z</d:Modified>
        <d:EditorId m:type="Edm.Int32">3974</d:EditorId>
        <d:OData__CopySource m:null="true" />
        <d:CheckoutUserId m:null="true" />
        <d:OData__UIVersionString>6.0</d:OData__UIVersionString>
        <d:GUID m:type="Edm.Guid">1bb37159-2438-4693-acab-ceef4b5a8214</d:GUID>
      </m:properties>
    </content>
  </entry>
  <link rel="next" href="https://portal.wow2.telenor.com/global/newscentre/_api/web/lists('b1b97552-232e-46e3-b9ec-f864a85f7177')/items?%24skiptoken=Paged%3dTRUE%26p_SortBehavior%3d0%26p_ID%3d1288&amp;%24%24orderby=Created+dsc" />
</feed>

我收到此错误:

org.xml.sax.SAXParseException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@4156ccc8) at the StringReader

此外,在将其转换为String后,在Logcat中显示整个XML文件时,我没有显示该文件。

1 个答案:

答案 0 :(得分:0)


问题是 StringReader 对象。将 InputStream 直接传递给文档对象

感谢所有分享知识的人