Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.db.xml;
  2.  
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.Element;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.NodeList;
  7. import org.xml.sax.SAXException;
  8.  
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;
  12. import java.io.File;
  13. import java.io.IOException;
  14.  
  15. public class ReadXML {
  16.  
  17. public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
  18. int flag = 0 ;
  19. DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance() ;
  20. File file = new File("C:\\Users\\Kimo Store\\Desktop\\ahmed.xml") ;
  21. DocumentBuilder builder = Factory.newDocumentBuilder() ;
  22. Document doc = builder.parse(file);
  23. NodeList RowList = doc.getElementsByTagName("Row");
  24. for (int i = 0 ; i < RowList.getLength() ; i++){
  25. Node R = RowList.item(i);
  26. if (R.getNodeType() == Node.ELEMENT_NODE){
  27. Element Row = (Element) R ;
  28. NodeList ColumnList = Row.getChildNodes();
  29. for (int j = 0 ; j<ColumnList.getLength() ; j++){
  30. Node C = ColumnList.item(j);
  31. if (C.getNodeType() == Node.ELEMENT_NODE){
  32. Element column = (Element) C ;
  33. if(i==0 && flag == 0) {
  34. System.out.print(column.getTagName() + " ");
  35. }
  36. else if (flag == 1 )
  37. System.out.print(column.getTextContent()+" ");
  38. }
  39. }
  40. System.out.println();
  41. if(i==0 && flag == 0){
  42. i-- ;
  43. flag =1 ;
  44. }
  45. }
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement