Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import Java.util.Map;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
Import Com.sun.javafx.sg.prism.web.NGWebView;
Import Sun.security.x509.KeyIdentifier;
public class GetXML {
Public Document GetDocument () {
Document Doc=null;
The absolute path to the XML file.
InputStream In=thread.currentthread (). Getcontextclassloader (). getResourceAsStream (". xml");
try {
Doc=documentbuilderfactory.newinstance (). Newdocumentbuilder (). Parse (in);
} catch (Saxexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Parserconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return doc;
}
public void GetTags () {
Document doc=this.getdocument ();
Gets the <action> element in the XML file.
NodeList nodelist=doc.getelementsbytagname ("action");
for (int i=0;i<nodelist.getlength (); i++) {
Element element= (Element) Nodelist.item (i);//a specific ACTION element
Gets the property value inside each action element. , you can encapsulate an object into a map key-value pair.
If the <action> tag is nested with other tags, you can go through the for loop.
String Name=element.getattribute ("name");
String Classname=element.getattribute ("class");
String Method=element.getattribute ("method");
}
}
}
If you get the contents of the tags inside the XML file in the Java class.