Jump to content

Recommended Posts

Posted

Boa noite, estou com um problema em ler um ficheiro xml para a construção de níveis de jogo. O curioso é que eu tentei em java sem ser pelo ADT e funcionou, mas no ADT diz sempre que nao consegue abrir já tentei colocar em vários directórios e nada. já coloquei na raiz do disco C e também já coloquei numa pasta dentro do eclipse e nada.

public class ReadXMLFile extends DefaultHandler{

Rect[] objs = new Rect[2];
String xPosS, yPosS, xSizeS, ySizeS;
float xPosI, yPosI, xSizeI, ySizeI;
int counter = 0;
 Rect[] ParseFile() {
 try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  DefaultHandler handler = new DefaultHandler() {
   boolean xPos = false;
   boolean yPos = false;
   boolean xSize = false;
   boolean ySize = false;
   public void startElement(String uri, String localName,
  String qName, Attributes attributes)
  throws SAXException {
 //System.out.println("Start Element :" + qName);
 if (qName.equalsIgnoreCase("XPOS")) {
  xPos = true;
 }
 if (qName.equalsIgnoreCase("YPOS")) {
  yPos = true;
 }
 if (qName.equalsIgnoreCase("XSIZE")) {
  xSize = true;
 }
 if (qName.equalsIgnoreCase("YSIZE")) {
  ySize = true;
 }
   }
   public void endElement(String uri, String localName,
  String qName) throws SAXException {
   }

   public void characters(char ch[], int start, int length)
  throws SAXException {
 if (xPos) {
  xPosS = new String(ch, start, length);
  xPosI = Float.valueOf(xPosS);
  xPos = false;
 }
 if (yPos) {
  yPosS = new String(ch, start, length);
  yPosI = Float.valueOf(yPosS);
  yPos = false;
 }
 if (xSize) {
  xSizeS = new String(ch, start, length);
  xSizeI = Float.valueOf(xSizeS);
  xSize = false;
 }
 if (ySize) {
  ySizeS = new String(ch, start, length);
  ySizeI = Float.valueOf(ySizeS);
  ySize = false;
  ConstructRectangle(xPosI, yPosI, xSizeI, ySizeI);
 }
   }
   private void ConstructRectangle(float xPosI, float yPosI, float xSizeI, float ySizeI) {
 // TODO Auto-generated method stub
 objs[counter] = new Rect(xPosI, yPosI, xSizeI, ySizeI);
 counter++;
   }


  };
  saxParser.parse("res/xml/file.xml", handler);
 } catch (Exception e) {
  e.printStackTrace();
 }

 return objs;
}
}

Obrigado pela atenção.

Cumprimentos.

Posted

Tens que o colocar na pasta assets. Depois utilizas a função getAssets() (http://developer.android.com/reference/android/content/res/Resources.html), que te dá um AssetManager. Daí podes abrir a InputStream para o ficheiro que podes passar directamente para o SAXParser.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted

Podes fazer getAssets() num Context. Uma Activity é um Context, logo se estiveres a fazer essa chamada numa Activity podes simplesmente chamar a função sem precisares de um objecto Resources.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted

Passas o Contexto (Activity) para essa classe. Adicionas uma variável Context à tua classe e um construtor:

public ReadXMLFile(Context context) {
   super();
   this.context = context;
}

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted

Eu diria que sim, mas pode depender muito da complexidade do problema.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.