Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java_dom_parser;
- import java.io.IOException;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- 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;
- public class MyDomParser {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse("configuration.xml");
- doc.getDocumentElement().normalize();
- NodeList simulationList = doc.getElementsByTagName("simulation");
- //NodeList cheminList = doc.getElementsByTagName("chemin");
- // iconList = doc.getElementsByTagName("icone");
- /*for (int k = 0; k < iconList.getLength(); k++) {
- Node ic = iconList.item(k);
- if(ic.getNodeType() == Node.ELEMENT_NODE) {
- Element icon = (Element) ic;
- String iconType = icon.getAttribute("type");
- String iconPath = icon.getAttribute("path");
- System.out.println(icon.getTagName() + " " + iconType + " " + iconPath);
- }
- }*/
- for (int i = 0; i < simulationList.getLength(); i++) {
- Node u = simulationList.item(i);
- if(u.getNodeType() == Node.ELEMENT_NODE) {
- Element usine = (Element) u;
- /* String type = usine.getAttribute("type");
- String id = usine.getAttribute("id");
- String posX = usine.getAttribute("x");
- String posY = usine.getAttribute("y");*/
- NodeList usineList = usine.getChildNodes();
- for (int j = 0; j < usineList.getLength(); j++) {
- Node n = usineList.item(j);
- if(n.getNodeType() == Node.ELEMENT_NODE) {
- Element name = (Element) n;
- String type = name.getAttribute("type");
- String id = name.getAttribute("id");
- String posX = name.getAttribute("x");
- String posY = name.getAttribute("y");
- System.out.println(type + " " + id + " " + posX +
- " " + posY );
- }
- // System.out.println(type + " "+ id + " "+ posX + " " +posY);
- }
- }
- }
- /*for (int j = 0; j < cheminList.getLength(); j++) {
- Node c = cheminList.item(j);
- if(c.getNodeType() == Node.ELEMENT_NODE) {
- Element chemin = (Element) c;
- String chemDe = chemin.getAttribute("de");
- String chemVers = chemin.getAttribute("vers");
- System.out.println(chemin.getTagName() + chemDe + " " + chemVers);
- }
- }*/
- } catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SAXException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement