Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using below method to parse an XML file
But My XML file is UFT-8
This is My Erro : Unexpected token (position:TEXT @1:4 in java.io.StringReader@529f6e70)


and My code for parse XML :
Java
public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setEncoding("ISO-8859-1"); //UTF-8 or 16
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);
            //doc = db.parse(new ByteArrayInputStream(xml.getBytes()));

        }
        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;
    }
Posted
Comments
[no name] 11-Oct-14 3:49am    
What is the XML content?
‫محم د‬‎ 11-Oct-14 3:55am    
some XML to Arabic or Persian Language Like this :

<rss version="2.0">
<channel>
<title>Feedpedia Today's World News</title>
<link>http://www.zuama.com</link>
<description>
The latest news and journals from all over the world.

<copyright>
Copyright 2005 - 2006 Feedpedia.com. All rights reserved.

<item>
<title>
«جاليمار»: فوز موديانو «انتصار للأدب الذي لا يقدر بثمن»
</title>
<description>
قال الناشر أنطوان جاليمار في حوار سريع أجرته معه مجلة «لونفيل أوبزرفاتور» إن فوز باتريك موديانو بنوبل للآداب 2014 هو فوز لدراه التي تتولى نشر أعماله لذلك هو يشعر بسعادة فائقة. هذا المحتوى من «المصري اليوم».. اضغط هنا لقراءة الموضوع الأصلي والتعليق عليه

<link>
http://92.62.182.57/zuama/WebForm.aspx?id=http://www.almasryalyoum.com/news/details/541393&itemID=45504
</link>
<pubDate>10/10/2014 10:12:00 PM</pubDate>


Richard MacCutchan 11-Oct-14 4:24am    
You should use a <byte> Stream so the parser can auto detect the encoding, as described in http://developer.android.com/reference/org/xml/sax/InputSource.html#getEncoding().

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900