Guest User

Untitled

a guest
Jul 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Reading xml file from java
  2. JAXBContext jc = JAXBContext.newInstance("myPackageName");
  3. //Create unmarshaller
  4. Unmarshaller um = jc.createUnmarshaller();
  5. //Unmarshal XML contents of the file myDoc.xml into your Java object instance.
  6. MyJAXBObject myJAXBObject = (MyJAXBObject)
  7. um.unmarshal(new java.io.FileInputStream( "myDoc.xml" ));
  8.  
  9. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  10. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  11. Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");
  12.  
  13. NodeList username = doc.getElementsByTagName("username");
  14. System.out.println("username ="+username.item(0).getTextContent());
  15. NodeList password = doc.getElementsByTagName("password");
  16. System.out.println("password ="+password.item(0).getTextContent());
  17.  
  18. public class TagHandler extends DefaultHandler{
  19.  
  20. //tag opening detection
  21. public void startElement(String uri, String localName,
  22. String qName, Attributes attributes) throws SAXException{
  23. // ... your code
  24. }
  25. //tag closing detection
  26. public void endElement(String uri, String localName, String qName)
  27. throws SAXException{
  28. // ... your code
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment