Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Reading xml file from java
- JAXBContext jc = JAXBContext.newInstance("myPackageName");
- //Create unmarshaller
- Unmarshaller um = jc.createUnmarshaller();
- //Unmarshal XML contents of the file myDoc.xml into your Java object instance.
- MyJAXBObject myJAXBObject = (MyJAXBObject)
- um.unmarshal(new java.io.FileInputStream( "myDoc.xml" ));
- DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
- Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");
- NodeList username = doc.getElementsByTagName("username");
- System.out.println("username ="+username.item(0).getTextContent());
- NodeList password = doc.getElementsByTagName("password");
- System.out.println("password ="+password.item(0).getTextContent());
- public class TagHandler extends DefaultHandler{
- //tag opening detection
- public void startElement(String uri, String localName,
- String qName, Attributes attributes) throws SAXException{
- // ... your code
- }
- //tag closing detection
- public void endElement(String uri, String localName, String qName)
- throws SAXException{
- // ... your code
- }
- }
Add Comment
Please, Sign In to add comment