/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.io.*; 

2. import javax.xml.parsers.*; 

3. import org.w3c.dom.*; 

4. import org.xml.sax.*; 

5. import java.util.logging.*; 

6. public class File2Dom 

7.

8.   public static void main(String[] args) 

9.   { 

10.     try

11.       DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); 

12.       DocumentBuilder bld = fac.newDocumentBuilder(); 

13.       System.out.println( bld.getClass().getName() ); 

14.        

15.       bld.setErrorHandler( new SAXHandler() ); 

16.       File f = new File("dom2file.xml"); 

17.       Document doc = bld.parse(f); 

18.  

19.       System.out.println("1."+ doc.hasChildNodes()); 

20.       Node nd = doc.getFirstChild(); 

21.       System.out.println("2."+ nd); 

22.        

23.       nd = nd.getNextSibling(); 

24.       System.out.println("3."+ nd); 

25.        

26.       NodeList nlst = nd.getChildNodes(); 

27.       for ( int i=0; i < nlst.getLength() ;i++ ) 

28.       { 

29.         System.out.println( "--("+i+")--" + nlst.item(i) ); 

30.       } 

31.        

32.       nd = nd.getFirstChild(); 

33.       System.out.print("4."+ nd); 

34.       System.out.print(", type : "+ nd.getNodeType()); 

35.       Element elm = (Element)nd ; 

36.       System.out.println(", attr. : "+ elm.getAttribute("atreebeauty")); 

37.       System.out.print(" Name : "+ elm.getNodeName() ); 

38.       System.out.println(", content : "+ elm.getTextContent()); 

39.        

40.       nd = nd.getFirstChild(); 

41.       System.out.print("5."+ nd); 

42.       System.out.print(", value : "+ nd.getNodeValue()); 

43.       System.out.println(", type : "+ nd.getNodeType()); 

44.        

45.       nd = nd.getParentNode().getParentNode(); 

46.       System.out.println("6."+ nd); 

47.        

48.       nd = nd.getLastChild().getLastChild().getPreviousSibling(); 

49.       System.out.println("7."+ nd); 

50.        

51.       nlst = doc.getElementsByTagName("aliment3"); 

52.       nd = nlst.item(0); 

53.       System.out.println("8."+ nd ); 

54.     } 

55.     catch (Exception e) {  e.printStackTrace();   } 

56.   } 

57.   static class SAXHandler implements ErrorHandler 

58.   { 

59.     private static Logger lgr = Logger.getLogger("com.javalessons"); 

60.     private static FileHandler h ; 

61.     static

62.       try

63.         h = new FileHandler( "log.txt", false ); 

64.         h.setFormatter(new SimpleFormatter()); 

65.         lgr.addHandler(h); 

66.       } 

67.       catch (Exception exc) { 

68.         exc.printStackTrace(); 

69.       } 

70.     } 

71.     public void fatalError(SAXParseException exc) 

72.     { 

73.       String m =  

74.         exc.getLineNumber() + "," + exc.getColumnNumber()  

75.           + " Fatal: " + exc.getMessage(); 

76.       lgr.log( Level.SEVERE, m ); 

77.     } 

78.     public void error(SAXParseException exc) 

79.     { 

80.       String m =  

81.         exc.getLineNumber() + "," + exc.getColumnNumber()  

82.           + " Error: " + exc.getMessage(); 

83.       lgr.log( Level.SEVERE, m ); 

84.     } 

85.     public void warning(SAXParseException exc) 

86.     { 

87.       String m =  

88.         exc.getLineNumber() + "," + exc.getColumnNumber()  

89.           + " Warning: " + exc.getMessage(); 

90.       lgr.log( Level.WARNING, m ); 

91.     } 

92.   } 

93.

94. /* === output : ======= 

95. com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl 

96. 1.true 

97. 2.[#comment: oh my, Steorn's ORBO] 

98. 3.[rootie: null

99. --(0)--[aliment2: null

100. --(1)--[aliment3: null

101. 4.[aliment2: null], type : 1, attr. : Valyou 

102.  Name : aliment2, content : ay 

103. 5.[#text: ay], value : ay, type : 3 

104. 6.[rootie: null

105. 7.[aliment4: null

106. 8.[aliment3: null

107.  

108. ===  dom2file.xml ===== 

109. <?xml version="1.0" encoding="UTF-8"?> 

110. <!--oh my, Steorn's ORBO--> 

111. <rootie> 

112.   <aliment2 atreebeauty="Valyou">ay</aliment2> 

113.   <aliment3> 

114.     bee 

115.     <aliment4 /> 

116.     <aliment5>sea</aliment5> 

117.   </aliment3> 

118. </rootie> 

119. */