16

i have trouble loading an xml file from assets directory. using the same line of code (just changing the path) i get different results ( either ok or NPE / file corrupted ) the file "castle1.tmx" (it's an xml file) is copied in two locations:

  • res/xml/castle1.tmx
  • assets/level/castle1.tmx

with this line, it works:

XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("res/xml/castle1.tmx");

while with this line it doesn't:

XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("assets/level/castle1.tmx");

i get the following result:

04-05 21:46:40.940: WARN/ResourceType(29056): Bad XML block: header size 28024 or total size 1702240364 is larger than data size 70441
04-05 21:46:40.940: ERROR/TestParser(29056): Unable to read resource file
04-05 21:46:40.940: WARN/System.err(29056): java.io.FileNotFoundException: Corrupt XML binary file
04-05 21:46:40.940: WARN/System.err(29056):     at android.content.res.AssetManager.openXmlAssetNative(Native Method)
04-05 21:46:40.944: WARN/System.err(29056):     at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
04-05 21:46:40.944: WARN/System.err(29056):     at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:453)
04-05 21:46:40.944: WARN/System.err(29056):     at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:442)
04-05 21:46:40.944: WARN/System.err(29056):     at game.test.MapLoader.<init>(MapLoader.java:73)

file is found in both case... it's just that i cannot seem to be able to read it from asset dir using that method..

any ideas how can i load my xml file from assets directory ?

tnx

freeaks
  • 487
  • 2
  • 6
  • 14

5 Answers5

19

In res/ folder all xml files are precompiled, whereas in assets/ folder they are not. So, you can't use openXmlResourceParser() with non-precompiled resources. Instead use open() and read file through InputStream.

GrAnd
  • 10,141
  • 3
  • 31
  • 43
  • so what could i use to parse an xml file in /assets/blah.xml ? do i have to search for keyword and atributes properties manually, creating my own parser ? no other options ? – freeaks Apr 05 '11 at 21:09
  • You can always use parsers given by Java such as XPath – Gerard Jul 01 '13 at 13:25
16

i succeded at loading and parsing my xml file from assets directory (assets/level/castle1.tmx)

here's what i did:

replaced this:

  XmlResourceParser xrp = ctx.getResources().getXml(ctx.getResources().getIdentifier(name, "xml", ctx.getPackageName()));

by this:

  InputStream istr = context.getAssets().open("level/"+name+".tmx");
  XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
  factory.setNamespaceAware(true); 
  xrp = factory.newPullParser(); 
  xrp.setInput(istr, "UTF-8");  

then all i had to do was editing some getAttributeIntValue() lines:

  int a = xrp.getAttributeIntValue(null, "width",0));

into this:

  int a = Integer.parseInt(xrp.getAttributeValue(null, "width"));

and all the rest worked without modifications :) ..this class is for parsing tiled xml/map files to build my game levels. before, it worked using res/ but i wanted to try to put all my files into assets/ instead. so now it works :)

thanks for the help

freeaks
  • 487
  • 2
  • 6
  • 14
4

The reason is because you are trying to load a binary XML file (your error is java.io.FileNotFoundException: Corrupt XML binary file).

All of the Android XML files (layouts, strings etc) stored in res are automatically compiled to binary XML when your project is compiled. XML files in assets are treated as standard XML files and so are not compiled to binary XML.

In summary: Android XML files must be in the res folder. You can only store plain-text XML in the assets folder (not layout files and such-like).

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • so, how do one should parse xml files residing inside assets/ directory ? should i load the whole xml file as a big string and start searching for keywords attributes properties and such by hand ? – freeaks Apr 05 '11 at 21:14
  • he was not talking about Android XML files, but about his own XML format – Heiko Rupp Apr 06 '11 at 05:26
  • @Heiko my bad - in that case *he* should be able to load standard XML files from the assets – Joseph Earl Apr 06 '11 at 10:01
3

Have a look at

https://github.com/pilhuhn/TurtleCar/blob/master/src/de/bsd/turtlecar/Board.java#L30 which is called from https://github.com/pilhuhn/TurtleCar/blob/master/src/de/bsd/turtlecar/SampleView.java#L45

for an example.

Basically you need to ask the AssetManager for the file:

 AssetManager assetManager = context.getAssets();
    try {
        InputStream is = assetManager.open("1.xml");
       ....
Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
0
    Resources resourcesForApplication = activity.getPackageManager().getResourcesForApplication(activity.getPackageName());
    AssetManager assets = resourcesForApplication.getAssets();
    InputStream xmlInputStream = assets.open("templates/horizontal/ybard/front/ybcard.xml");

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parseXml = factory.newPullParser();

    parseXml.setInput(xmlInputStream ,"UTF-8");



    while ((eventType = parseXml.nextToken()) != XmlPullParser.END_DOCUMENT){

        if (eventType == XmlPullParser.START_TAG && parseXml.getName().equals("layers")) {

            String documentWidth = parseXml.getAttributeValue(0);
            String documentHeight = parseXml.getAttributeValue(1);
            ...
Stav Bodik
  • 2,018
  • 3
  • 18
  • 25