Import java. Io. file;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import org. W3C. Dom .*;
Import org. xml. Sax. saxexception;
Import javax. xml. parsers .*;
Import javax. xml. Transform .*;
Import javax. xml. Transform. Dom. domsource;
Import javax. xml. Transform. Stream .*;
Import javax. xml. XPath .*;
Public class testxmlroles {
Public static void main (string [] ARGs ){
If (ARGs. length> 0 ){
String method = ARGs [0];
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
// Root Node
Element root = NULL;
// Subnode
Element therole = NULL;
// Subnode Element
Element theelem = NULL;
Try {
Factory. setignoringelementcontentwhitespace (true );
Documentbuilder DB = factory. newdocumentbuilder ();
Document xmldoc = dB. parse (new file ("test. xml "));
Root = xmldoc. getdocumentelement ();
// --- Create a role (subnode )----
// Call format: Java testxmlroles add newrole 12 123456.
If (method. Equals ("add ")){
If (ARGs. length! = 4 ){
System. Out. println ("Enter four parameters, for example, Java testxmlroles add newrole 12 123456 ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [name = '" + ARGs [1] + "' and ID = '" + ARGs [2] + "']", root );
If (therole! = NULL ){
System. Out. println ("--- this name or ID already exists. Set another ----");
} Else {
Therole = xmldoc. createelement ("role ");
// Add a subnode Element
Theelem = xmldoc. createelement ("name ");
Theelem. settextcontent (ARGs [1]);
Therole. appendchild (theelem );
Theelem = xmldoc. createelement ("ID ");
Theelem. settextcontent (ARGs [2]);
Therole. appendchild (theelem );
Theelem = xmldoc. createelement ("Pas ");
Theelem. settextcontent (ARGs [3]);
Therole. appendchild (theelem );
// Add a child node to the root node
Root. appendchild (therole );
Output (xmldoc );
Savexml ("test. xml", xmldoc );
System. Out. println ("--- added successfully ----");
}
}
}
// --- Delete a role using the ID attribute ----
// Call format: Java testxmlroles delbyid 12
Else if (method. Equals ("delbyid ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the ID in the format of --> JAVA testxmlroles delbyid 12 ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [ID = '" + ARGs [1] + "']", root );
If (therole! = NULL ){
Therole. getparentnode (). removechild (therole );
Savexml ("test. xml", xmldoc );
System. Out. println ("--- deleted successfully ----");
} Else {
System. Out. println ("-- the role with this ID does not exist ----");
}
}
}
// --- Delete a role using the name attribute ----
// Call format: Java testxmlroles delbyname newrole
Else if (method. Equals ("delbyname ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the name parameter in the format of --> JAVA testxmlroles delbyname newrole ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [name = '" + ARGs [1] + "']", root );
If (therole! = NULL ){
Therole. getparentnode (). removechild (therole );
Savexml ("test. xml", xmldoc );
System. Out. println ("--- deleted successfully ----");
} Else {
System. Out. println ("--- the role corresponding to the name does not exist ----");
}
}
}
// Search for ID and PAS by name
// Call format: Java testxmlroles findbyname newrole
Else if (method. Equals ("findbyname ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the name parameter in the format of --> JAVA testxmlroles findbyname newrole ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [name = '" + ARGs [1] + "']", root );
If (therole! = NULL ){
// Getelementsbytagname returns nodelist, so keep up with item (0)
String Password = therole. getelementsbytagname ("Pas"). Item (0). gettextcontent ();
String id = therole. getelementsbytagname ("ID"). Item (0). gettextcontent ();
System. Out. println ("--- corresponding role ID and password: ----" + ID + "," + password );
} Else {
System. Out. println ("--- the role with this name does not exist ---");
}
}
}
// Search for name and PAS by ID
// Call format: Java testxmlroles findbyid 30
Else if (method. Equals ("findbyid ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the ID parameter in the format of --> JAVA testxmlroles findbyid 30 ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [ID = '" + ARGs [1] + "']", root );
If (therole! = NULL ){
// Getelementsbytagname returns nodelist, so keep up with item (0)
String Password = therole. getelementsbytagname ("Pas"). Item (0). gettextcontent ();
String name = therole. getelementsbytagname ("name"). Item (0). gettextcontent ();
System. Out. println ("--- corresponding role ID and password: ----" + name + "," + password );
} Else {
System. Out. println ("--- no relevant record found ---");
}
}
}
// --- Modify the role according to the name. ----
// Call format: Java testxmlroles editbyname CS 8888888
Else if (method. Equals ("editbyname ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the name parameter in the format of --> JAVA testxmlroles editbyname CS 8888888 ");
} Else {
Therole = (element) selectsinglenode ("/roles/role [name = '" + ARGs [1] + "']", root );
If (therole! = NULL ){
// --- Change the role password -----
// Getelementsbytagname returns nodelist, so keep up with item (0)
Therole. getelementsbytagname ("Pas"). Item (0). settextcontent (ARGs [2]);
/* // --- Add an attribute 'id' to the node. The value is B01 ----
Therole. setattribute ("ID", "B01 ");
Output (therole );*/
Savexml ("test. xml", xmldoc );
} Else {
System. Out. println ("--- no relevant record found ---");
}
}
}
// Batch Delete
// Call the Java testxmlroles delbanch id <10
Else if (method. Equals ("delbanch ")){
If (ARGs. length! = 2 ){
System. Out. println ("Enter the deletion condition in the format of --> JAVA testxmlroles delbanch id <10 ");
} Else {
// Nodelist someroles = selectnodes ("/roles/role [ID <10]", root );
Nodelist someroles = selectnodes ("/roles/role [" + ARGs [2] + "]", root );
System. Out. println ("--- qualified roles include" + someroles. getlength () + ---");
For (INT I = 0; I <someroles. getlength (); I ++ ){
Someroles. Item (I). getparentnode (). removechild (someroles. Item (I ));
}
Savexml ("test. xml", xmldoc );
}
} Else {
System. Out. println ("--- enter the correct method name ---");
}
} Catch (parserconfigurationexception e ){
E. printstacktrace ();
} Catch (saxexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
} Else {
System. Out. println ("to call this program, you need to enter the relevant parameters: Format example --> JAVA testxmlroles add newrole 12 123456 ");
}
}
// Output the XML string of node to the console
Public static void output (node ){
Transformerfactory transfactory = transformerfactory. newinstance ();
Try {
Transformer transformer = transfactory. newtransformer ();
Transformer. setoutputproperty ("encoding", "gb2312 ");
Transformer. setoutputproperty ("indent", "yes ");
Domsource source = new domsource ();
Source. setnode (node );
Streamresult result = new streamresult ();
Result. setoutputstream (system. Out );
Transformer. Transform (source, result );
} Catch (transformerconfigurationexception e ){
E. printstacktrace ();
} Catch (transformerexception e ){
E. printstacktrace ();
}
}
// Search for the node and return the first qualified Node
Public static node selectsinglenode (string Express, object source ){
Node result = NULL;
Xpathfactory = xpathfactory. newinstance ();
XPath = xpathfactory. newxpath ();
Try {
Result = (node) XPath. Evaluate (Express, source, xpathconstants. node );
} Catch (xpathexpressionexception e ){
E. printstacktrace ();
}
Return result;
}
// Find the node and return the node set that meets the condition.
Public static nodelist selectnodes (string Express, object source ){
Nodelist result = NULL;
Xpathfactory = xpathfactory. newinstance ();
XPath = xpathfactory. newxpath ();
Try {
Result = (nodelist) XPath. Evaluate (Express, source, xpathconstants. nodeset );
} Catch (xpathexpressionexception e ){
E. printstacktrace ();
}
Return result;
}
// Output the document to the file
Public static void savexml (string filename, document DOC ){
Transformerfactory transfactory = transformerfactory. newinstance ();
Try {
Transformer transformer = transfactory. newtransformer ();
Transformer. setoutputproperty ("indent", "yes ");
Domsource source = new domsource ();
Source. setnode (DOC );
Streamresult result = new streamresult ();
Result. setoutputstream (New fileoutputstream (filename ));
Transformer. Transform (source, result );
} Catch (transformerconfigurationexception e ){
E. printstacktrace ();
} Catch (transformerexception e ){
E. printstacktrace ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
}
}
}
XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Roles>
<Role>
<Name> Zhang San </Name>
<ID> 10 </ID>
<Pas> 789456 </PAS>
</Role>
<Role>
<Name> Li Si </Name>
<ID> 20 </ID>
<Pas> 654321 </PAS>
</Role>
<Role id = "B01">
<Name> CS </Name>
<ID> 30 </ID>
<Pas> 8888888 </PAS>
</Role>
</Roles>