org.w3c.dom对XML的操作示例

自己写的实际代码,保留下来,以供参考

==XML===============================================================

<?xml version="1.0" encoding="UTF-8"?>
<tenants>
<tenant>
<tenantID>tenantID1</tenantID>
<jndi>jdbc/PlatformDS</jndi>
</tenant>
<tenantID>tenantID2</tenantID>
<jndi>jdbc/PlatformDS2</jndi>
<tenant>
</tenant>
</tenants>

===JAVA==============================================================

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class TenantJNDIUtil {

private static String filePath="D:/AppSrv01/config/JNDI.xml";

//private static TenantJNDIUtil singleton;

public static TenantJNDIUtil getInstance()
{
//if(singleton==null)
// singleton=new TenantJNDIUtil();
return new TenantJNDIUtil();
}

public org.w3c.dom.Document getDoc()
{
org.w3c.dom.Document doc=null;
File file = new File(filePath);
if(!file.exists())
{
try {
file.createNewFile();
FileWriter writer=new FileWriter(file,true);
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><tenants></tenants>");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc=db.parse(file);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}

public boolean saveDoc(org.w3c.dom.Document doc)
{
boolean saved=true;

try {
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (Exception e){
saved=false;
}

return saved;
}

public List<String> getAllTenants()
{
List<String> tenants=new ArrayList<String>();

org.w3c.dom.Document doc=getDoc();
NodeList list=doc.getElementsByTagName("tenant");
for(int index=0;index<list.getLength();index++)
{
Element node=(Element)list.item(index);
NodeList tenant=node.getElementsByTagName("tenantID");
tenants.add(tenant.item(0).getFirstChild().getNodeValue());
}
return tenants;
}

public Map<String,String> getAllTenantsJNDI()
{
Map<String,String> tenantsJNDI=new HashMap<String,String>();

org.w3c.dom.Document doc=getDoc();
NodeList list=doc.getElementsByTagName("tenant");
for(int index=0;index<list.getLength();index++)
{
Element node=(Element)list.item(index);
NodeList tenant=node.getElementsByTagName("tenantID");
NodeList jndi=node.getElementsByTagName("jndi");
tenantsJNDI.put(tenant.item(0).getFirstChild().getNodeValue(), jndi.item(0).getFirstChild().getNodeValue());
}

return tenantsJNDI;
}

public List<String> getAllJNDI()
{
List<String> jndis=new ArrayList<String>();
jndis.addAll(getAllTenantsJNDI().values());
return jndis;
}

public boolean ifExistTenant(String tenantID)
{
boolean ifExist=false;
if(getAllTenants().contains(tenantID.toUpperCase()))
ifExist=true;
return ifExist;
}

public boolean ifExistJNDI(String jndi)
{
return getAllJNDI().contains(jndi);
}

public boolean addTenantJNDI(String tenantID,String jndi)
{
if(ifExistTenant(tenantID)||ifExistJNDI(jndi))
return false;

org.w3c.dom.Document doc=getDoc();

Node root=doc.getElementsByTagName("tenants").item(0);

Element ELEtenant=doc.createElement("tenant");
root.appendChild(ELEtenant);

Element ELEtenantID=doc.createElement("tenantID");
Text TtenantID=doc.createTextNode(tenantID.toUpperCase());
ELEtenantID.appendChild(TtenantID);
ELEtenant.appendChild(ELEtenantID);

Element ELEjndi=doc.createElement("jndi");
Text Tjndi=doc.createTextNode(jndi);
ELEjndi.appendChild(Tjndi);
ELEtenant.appendChild(ELEjndi);

return saveDoc(doc);
}

public boolean deleteTenantJNDI(String tenantID)
{
if(!ifExistTenant(tenantID))
return true;

org.w3c.dom.Document doc=getDoc();


Node tenants=doc.getFirstChild();

NodeList children=tenants.getChildNodes();

for(int index=0;index<children.getLength();index++)
{
if(children.item(index) instanceof Element)
{
Element node=(Element)children.item(index);
NodeList tenant=node.getElementsByTagName("tenantID");
if(tenant.item(0).getFirstChild().getNodeValue().equalsIgnoreCase(tenantID))
{
tenants.removeChild(tenants.getChildNodes().item(index));
break;
}
}
}
return saveDoc(doc);
}

public boolean replaceTenantJNDI(String tenantID, String newJNDI)
{
if(!ifExistTenant(tenantID))
return addTenantJNDI(tenantID,newJNDI);
if(ifExistJNDI(newJNDI))
return false;
org.w3c.dom.Document doc=getDoc();
NodeList list=doc.getElementsByTagName("tenant");
for(int index=0;index<list.getLength();index++)
{
Element node=(Element)list.item(index);
NodeList tenant=node.getElementsByTagName("tenantID");
NodeList jndi=node.getElementsByTagName("jndi");
if(tenant.item(0).getFirstChild().getNodeValue().equalsIgnoreCase(tenantID))
{
jndi.item(0).getFirstChild().setNodeValue(newJNDI);
break;
}
}
return saveDoc(doc);
}

}

你可能感兴趣的:(xml,jdbc)