Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <classe>
  3. <grade index="infancia">
  4. <descricao>a infancia</descricao>
  5. <topicos>
  6. <topico titulo="crescimento">
  7. <item>De 0 a 5 anos</item>
  8. <item>De 5 a 12 anos</item>
  9. <item>De 12 a 14 anos</item>
  10. <item>14 anos</item>
  11. </topico>
  12. <topico titulo="pre-adolecência">
  13. <item>Odeio minha mãe</item>
  14. <item>Preciso da minha mãe</item>
  15. <item>Me pai, Ha pai</item>
  16. <item>Tesouros escondidos</item>
  17. </topico>
  18. </topicos>
  19. </grade>
  20.  
  21. <grade index="adolescencia">
  22. <descricao>A adolescência</descricao>
  23. <topicos>
  24. <topico titulo="adolecencia">
  25. <item>Janelas com flores</item>
  26. <item>cozinha da minha mae, limpa e arrumada</item>
  27. <item>Vinni</item>
  28. <item>95</item>
  29. </topico>
  30. <topico titulo="amor">
  31. <item>vá lá, quem precisa</item>
  32. <item>vamos lá, todos nos</item>
  33. <item>O amor é tudo</item>
  34. <item>Passarinhos cantando</item>
  35. </topico>
  36. </topicos>
  37. </grade>
  38.  
  39. import java.io.File;
  40. import javax.xml.parsers.DocumentBuilder;
  41. import javax.xml.parsers.DocumentBuilderFactory;
  42. import org.w3c.dom.Document;
  43. import org.w3c.dom.NamedNodeMap;
  44. import org.w3c.dom.Node;
  45. import org.w3c.dom.NodeList;
  46.  
  47. public class ReadXMLFile2 {
  48.  
  49. public static void main(String[] args) {
  50. rum("cozinha");
  51. }
  52.  
  53. private static void rum(String phrase) {
  54. try {
  55. File file = new File("/xxx/data/temas.xml");
  56. DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  57. Document doc = dBuilder.parse(file);
  58. System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
  59. NodeList root = doc.getChildNodes();
  60. Node book = getNode("classes", root);
  61. printNote(book.getChildNodes());
  62. } catch (Exception e) {
  63. System.out.println(e.getMessage());
  64. }
  65.  
  66. }
  67.  
  68. protected static Node getNode(String tagName, NodeList nodes) {
  69. for (int x = 0; x < nodes.getLength(); x++) {
  70. Node node = nodes.item(x);
  71. if (node.getNodeName().equalsIgnoreCase(tagName)) {
  72. return node;
  73. }
  74. }
  75.  
  76. return null;
  77. }
  78.  
  79. private static void printNote(NodeList nodeList) {
  80.  
  81. Node tmp = null;
  82. for (int n = 0; n < nodeList.getLength(); n++) {
  83.  
  84. Node tempNode = nodeList.item(n);
  85. if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
  86.  
  87. if (tempNode.hasAttributes()) {
  88. // get attributes names and values
  89. NamedNodeMap nodeMap = tempNode.getAttributes();
  90.  
  91. for (int i = 0; i < nodeMap.getLength(); i++) {
  92.  
  93. Node node = nodeMap.item(i);
  94. System.out.println("<div id="" + node.getNodeValue() + "">");
  95.  
  96. }
  97. System.out.println("<div id="" + tempNode.getNodeValue() + "">" + tempNode.getTextContent());
  98. tmp = null;
  99. }
  100. }
  101. }
  102. }
  103. }
  104.  
  105. private Classe getClasseFromXml(String xml) {
  106. XStream stream = new XStream(new DomDriver());
  107. stream.alias("tag1", Items.class);
  108. stream.alias("tag2", Classe.class);
  109. return (Classe) stream.fromXML(xml);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement