import java.io.IOException;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import buildingInfo.Level;
public class XmlParser {
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Vector<Level> vLvl = new Vector<Level>(30);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("C:\\Users\\BeatriceGhetel\\workspace\\IPProject\\src\\configuration\\Configure.xml");
NodeList itemList = doc.getElementsByTagName("LEVEL");
for (int i = 0; i < itemList.getLength(); i++) {
Node it = itemList.item(i);
if (it.getNodeType()==Node.ELEMENT_NODE) {
Element el = (Element) it;
NodeList L = el.getChildNodes();
for (int j=0; j< L.getLength(); j++) {
Node n = L.item(j);
if (n.getNodeType()==Node.ELEMENT_NODE) {
buildingInfo.Level Lvl = new BuildingInfo.Level();
if (n.getNodeName().matches("HEIGHT")) {
Lvl.Height = Float.parseFloat(n.getTextContent());
}
System.out.println(Lvl.Height + " inaltime nivel");
Element e = (Element) n;
}
}
}
}
} catch (Exception ex){
ex.printStackTrace();
}
}
}
<BUILDING>
<LEVEL>
<LABEL> 0 </LABEL>
<HEIGHT> 2.5 </HEIGHT>
<WIDTH> 9 </WIDTH>
<LENGHT> 8 </LENGHT>
<CAPACITY> 120 </CAPACITY>
<ROOM>
<LABEL> 1 </LABEL>
<TYPE> NORMALROOM </TYPE>
<HEIGHT> 2.5 </HEIGHT>
<WIDTH> 5 </WIDTH>
<LENGTH> 5 </LENGTH>
<CAPACITY> 40 </CAPACITY>
<SPRINKLER> YES </SPRINKLER>
<SMOKEDETECTOR> YES </SMOKEDETECTOR>
<EXTINGUISHERNUMBER> 2 </EXTINGUISHERNUMBER>
<DOORNUMBER> 1 </DOORNUMBER>
<DOOR>
<ID> 1 </ID>
<TYPE > EXTERNAL </TYPE >
<CAPACITY> 2 </CAPACITY>
<POSITION> NORTH </POSITION>
</DOOR>
</ROOM>
I try to create a model of a building getting attributes from a XML document of configuration. The problem I have is that I cannot make a difference between the height of a room or height of a door, and it also doesn't always take the height, it sometimes take other values like length instead of height. I also need to figure out how to create a collection of rooms, and to create objects for the nested items like Doors.