Reputation: 71
I am trying to create a GUI interface for user login in java swing. I am trying to store my data in an Xml file and I need to access it from java and check if the user name and password entered in the GUI matches with any of the given user details in the xml data. If it matches they are given access to the next level.
I am trying to use SAX parser for accessing the Xml file . I am unable to understand how the SAX parser works and how it can be used for matching with the input from the GUI.
Please help. Thanks for all your help in advance.
Upvotes: 1
Views: 1432
Reputation: 163
SAX Parser works differently from DOM/JAXB. With DOM, the XML content wil accessible via DOM generated node/element. Using SAX, the parser will read through your XML file, and than via event it will notice the content handler such like :
With information such as tag attributes, content etc.
Example :
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
}
}
There is more than that two events mentioned.
I Hope that will help you to understand SAX better
Upvotes: 0
Reputation: 853
You can do this way
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());
Upvotes: 0
Reputation: 1646
you could use Jaxb if you have model your document:
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" ));
you can easily validate it if you have a schema of your model
here there is an example
http://www.mkyong.com/java/jaxb-hello-world-example/
Upvotes: 2
Reputation: 2014
for each of your GUI elements you should get text and for open XML and parse it check THIS CODE out, it will be helpful
Upvotes: 0