# Get sprites from XML and spritesheet in Java

I have a SpriteSheet that have a lot of images in one in .png format, and the XML file with the all names of every sprite (x,y,width,height) like this:

 <TextureAtlas imagePath="sheet.png">

 <SubTexture name="box.png" x="0" y="864" width="70" height="70"/> 

</TextureAtlas> 

Im using Slick2D to make a BasicGame, SpriteSheet and XStream lib to manage the sprites, but i cant do, that load the image correctly. How I can do?

You have to parse the xml file. It's not hard thing to do and this should get you started.

File file = new File("spritesheet.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
NodeList subTextureNodeList = document.getElementsByTagName("SubTexture");


Now we have all the subtexture-data of spritesheet stored in subTextureList as an XML node. To read name,x,y, width and height of all images, do this:

for(int i = 0 ; i < subTextureNodeList.getLength(); i++){
Element subTextureElement = (Element)subTextureNodeList.item(i);
String name = subTextureElement.getAttribute("name");
String x= subTextureElement.getAttribute("x");
String y= subTextureElement.getAttribute("y");
String width = subTextureElement.getAttribute("width");
String height= subTextureElement.getAttribute("height");

// DO SOMETHING WITH THIS DATA HERE
// GET THE PICTURE FROM SHEET WITH THESE.
}
}