2

My goal is to download xml file from url and parse it. For experiments, I use next xml file: https://www.dropbox.com/s/l5qtpcrryuistpk/Adtest.xml

<?xml version="1.0" encoding="UTF-8"> 
<adsettings> 
<adnetwork>AdMob</adnetwork> 
<publisherid>12345</publisherid> 
</adsettings>

Firstly, I download it with this code:

URL xmlUrl = new URL(XML_SETTINGS_URL);
HttpURLConnection urlConnection = (HttpURLConnection) xmlUrl.openConnection();
urlConnection.connect();
FileOutputStream fileOutputStream = adContext.openFileOutput(SETTINGS_FILENAME,
                adContext.MODE_PRIVATE);

InputStream stream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];

while (stream.read(buffer) > -1) {
    fileOutputStream.write(buffer);
        }

fileOutputStream.close();
urlConnection.disconnect();

Next, I`m trying to parse it:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

XmlPullParser parser = factory.newPullParser();
File file = adContext.getFileStreamPath(SETTINGS_FILENAME);
FileReader fileReader = new FileReader(file);
System.out.println(fileReader);
parser.setInput(fileReader);
int eventType = parser.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {
    if(eventType == XmlPullParser.START_DOCUMENT) {
       Log.d(TAG, "Start document");
    } else if(eventType == XmlPullParser.START_TAG) {
        Log.d(TAG, "Start tag "+parser.getName());
    } else if(eventType == XmlPullParser.END_TAG) {
        Log.d(TAG, "End tag "+parser.getName());
    } else if(eventType == XmlPullParser.TEXT) {
        Log.d(TAG, "Text "+parser.getText());
    }
    eventType = parser.next();
}

As a result, I receive only Start document, html, head and script, followed by exception:

org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT @13:51 in      java.io.FileReader@4287eae8)

Any ideas how to solve it? Thank you in advance

kaderud
  • 5,457
  • 2
  • 36
  • 49
  • To isolate the issue hardcode the Xml content in a string variable (xmlString) and directly give it to the parser. You can use parser.setInput(xmlStream, "utf-8") and xmlStream = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8)); – naveejr Jun 26 '14 at 13:19
  • the link to Adtest.xml doesn't return XML. It returns HTML instead. Try it in Postman or something similar and see the raw data coming back. – Streets Of Boston Jun 26 '14 at 13:22
  • @naveejr, hardcoded string was initial setup, it is ok. – Gennadiy Kartashevskyy Jun 26 '14 at 13:35
  • @StreetsOfBoston, could you advice utility to create proper xml file? – Gennadiy Kartashevskyy Jun 26 '14 at 13:39
  • In which folder did you put your xml file on your dropbox? I believe it will only work if you put it in your Public dropbox directory, and then make sure you get the correct URL. Also, what if you try the following URL `https://www.dropbox.com/s/l5qtpcrryuistpk/Adtest.xml?dl=1` in your program. (you can use *http* instead if you like) @GennadiyKartashevskyy – kaderud Jun 26 '14 at 15:21
  • @kaderud, thanks! Adding of ?dl=1 helped! ) – Gennadiy Kartashevskyy Jun 26 '14 at 16:04

2 Answers2

5

I only answered this question in a comment, so here's the real answer done right, so your question can be resolved!

How do I force a file to download from Dropbox?

There are a couple simple modifications to Dropbox link URLs you can do to make sure browsers handle your Dropbox links in the way you want.

Render a file in your browser

Most modern web browsers can automatically detect, open, and otherwise handle many file types. For instance, clicking image links in a browser will typically open the image within the browser rather than download it to your desktop.

To bypass the preview page and allow your browser to handle your files, replace www with dl and dropbox with dropboxusercontent in your URL. For example:

https://www.dropbox.com/s/xttkmuvu7hh72vu/MyFile.pdf

Becomes:

https://dl.dropboxusercontent.com/s/xttkmuvu7hh72vu/MyFile.pdf

Force a file or folder to download

Using the new direct link above, you can further modify the URL to force the browser to download the file, rather than handle it natively. To force a file download, append a direct link with ?dl=1. For example:

https://dl.dropboxusercontent.com/s/xttkmuvu7hh72vu/MyFile.pdf

(Note that the URL begins with dl.dropboxusercontent.com, as instructed in the section above.)

Becomes:

https://dl.dropboxusercontent.com/s/xttkmuvu7hh72vu/MyFile.pdf?dl=1

Enter the new URL in the address field of your browser and it should automatically download, instead of open, the file.

(reference: [above text copied from dropbox help] https://www.dropbox.com/help/201/en)

kaderud
  • 5,457
  • 2
  • 36
  • 49
  • how many times can I request a file from dropbox? is there a limit? I mean, I have an android app and I need to download a single txt file from Dropbox – Orlando Herrera Apr 05 '16 at 15:56
  • Your answer about how to read a file from DBox help me a lot!! – Orlando Herrera Apr 05 '16 at 15:56
  • @OrlandoHerrera Dropbox Basic (free) accounts; The total amount of _traffic_ that all of your links and file requests together can generate without getting banned is **20 GB** _per day_. The total number of _downloads_ that all of your links together can generate is **100,000 downloads** _per day_. https://www.dropbox.com/en/help/4204 – kaderud Apr 05 '16 at 16:00
  • I will take a look in your link. Thanks a lot, friend !! @kaderud – Orlando Herrera Apr 05 '16 at 16:02
0

There is a "?" mark missing

 <?xml version="1.0" encoding="UTF-8">  

should be like

 <?xml version="1.0" encoding="UTF-8"?>  
naveejr
  • 735
  • 1
  • 15
  • 31