Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package java_dom_parser;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.parsers.ParserConfigurationException;
  8.  
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.Node;
  12. import org.w3c.dom.NodeList;
  13. import org.xml.sax.SAXException;
  14.  
  15. public class MyDomParser {
  16.  
  17. public static void main(String[] args) {
  18. // TODO Auto-generated method stub
  19. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  20. try {
  21. DocumentBuilder db = dbf.newDocumentBuilder();
  22. Document doc = db.parse("configuration.xml");
  23. doc.getDocumentElement().normalize();
  24. NodeList simulationList = doc.getElementsByTagName("simulation");
  25. //NodeList cheminList = doc.getElementsByTagName("chemin");
  26.  
  27. // iconList = doc.getElementsByTagName("icone");
  28.  
  29. /*for (int k = 0; k < iconList.getLength(); k++) {
  30. Node ic = iconList.item(k);
  31. if(ic.getNodeType() == Node.ELEMENT_NODE) {
  32. Element icon = (Element) ic;
  33. String iconType = icon.getAttribute("type");
  34. String iconPath = icon.getAttribute("path");
  35. System.out.println(icon.getTagName() + " " + iconType + " " + iconPath);
  36.  
  37. }
  38.  
  39. }*/
  40.  
  41.  
  42.  
  43. for (int i = 0; i < simulationList.getLength(); i++) {
  44. Node u = simulationList.item(i);
  45. if(u.getNodeType() == Node.ELEMENT_NODE) {
  46. Element usine = (Element) u;
  47. /* String type = usine.getAttribute("type");
  48. String id = usine.getAttribute("id");
  49. String posX = usine.getAttribute("x");
  50. String posY = usine.getAttribute("y");*/
  51.  
  52. NodeList usineList = usine.getChildNodes();
  53. for (int j = 0; j < usineList.getLength(); j++) {
  54. Node n = usineList.item(j);
  55. if(n.getNodeType() == Node.ELEMENT_NODE) {
  56. Element name = (Element) n;
  57. String type = name.getAttribute("type");
  58. String id = name.getAttribute("id");
  59. String posX = name.getAttribute("x");
  60. String posY = name.getAttribute("y");
  61. System.out.println(type + " " + id + " " + posX +
  62. " " + posY );
  63.  
  64. }
  65. // System.out.println(type + " "+ id + " "+ posX + " " +posY);
  66.  
  67. }
  68. }
  69. }
  70.  
  71. /*for (int j = 0; j < cheminList.getLength(); j++) {
  72. Node c = cheminList.item(j);
  73. if(c.getNodeType() == Node.ELEMENT_NODE) {
  74. Element chemin = (Element) c;
  75. String chemDe = chemin.getAttribute("de");
  76. String chemVers = chemin.getAttribute("vers");
  77. System.out.println(chemin.getTagName() + chemDe + " " + chemVers);
  78.  
  79. }
  80.  
  81. }*/
  82. } catch (ParserConfigurationException e) {
  83. // TODO Auto-generated catch block
  84. e.printStackTrace();
  85. } catch (SAXException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. } catch (IOException e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. }
  92.  
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement