Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <personaldetails>
  3. <phno>1553294232</phno>
  4. <email>
  5. <official>xya@gmail.com</official>
  6. <personal>bk@yahoo.com</personal>
  7. </email>
  8. </personaldetails>
  9.  
  10. 1553294232
  11. xya@gmail.com
  12. bk@yahoo.com
  13.  
  14. public void parseXML(){
  15. try{
  16. DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  17. Document doc;
  18.  
  19. doc = documentBuilder.parse(new File("test.xml"));
  20. getData(null, doc.getDocumentElement());
  21. }catch(Exception exe){
  22. exe.printStackTrace();
  23. }
  24. }
  25.  
  26. private void getData(Node parentNode, Node node){
  27.  
  28. switch(node.getNodeType()){
  29. case Node.ELEMENT_NODE:{
  30.  
  31. if(node.hasChildNodes()){
  32. NodeList list = node.getChildNodes();
  33. int size = list.getLength();
  34.  
  35. for(int index = 0; index < size; index++){
  36. getData(node, list.item(index));
  37. }
  38. }
  39.  
  40. break;
  41. }
  42.  
  43. case Node.TEXT_NODE:{
  44. String data = node.getNodeValue();
  45.  
  46. if(data.trim().length() > 0){
  47. /*
  48. * Here you need to check the data against your ruleset and perform your operation
  49. */
  50. System.out.println(parentNode.getNodeName()+" :: "+node.getNodeValue());
  51. }
  52. break;
  53. }
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement