简单的替换操作,基本不需引jar包。
首先写一个工具类。
package xuzs.servlet; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public final class DomUtils { public static Document file2Dom(String fileName) { String strXml = file2Str(fileName); Document doc = str2Dom(strXml); return doc; } public static String dom2Str(Document doc) { ByteArrayOutputStream bos = null; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty("encoding", "utf-8"); bos = new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(bos)); } catch (TransformerException e) { e.printStackTrace(); } return bos.toString(); } public static Document str2Dom(String xmlStr) { StringReader sr = new StringReader(xmlStr); InputSource is = new InputSource(sr); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document doc = null; try { builder = factory.newDocumentBuilder(); doc = builder.parse(is); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return doc; } public static String file2Str(String fileName) { StringBuilder sb = new StringBuilder(); BufferedReader br = null; try { FileReader fr = new FileReader(fileName); br = new BufferedReader(fr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); sb.append("\n"); } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
简单的测试类。
package xuzs.servlet; import org.apache.soap.SOAPException; import org.w3c.dom.Document; import org.w3c.dom.Node; public class DomTest { private static final String DEFAULT_DATA_FILENAME = "D:/AuthzTestQuery.xml"; public static void main(String[] args) throws SOAPException { Document doc = DomUtils.file2Dom(DEFAULT_DATA_FILENAME); System.out.println(DomUtils.dom2Str(doc)); Node node = doc.getElementsByTagName("samlp:AuthzDecisionQuery").item(0); // 获取指定属性 String namespaceURI = node.getAttributes().getNamedItem("xmlns:samlp").getNodeValue(); System.out.println(namespaceURI); doc.renameNode(node, namespaceURI, "samlp:activateRequest"); System.out.println(DomUtils.dom2Str(doc)); } }
被处理的文件。
<?xml version="1.0" encoding="UTF-8" ?> <samlp:AuthzDecisionQuery xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="AuthzQuery12345789" IssueInstant="2009-08-14T15:22:03.250Z" Resource="http://mycom.com/Repository/Private" Version="2.0"> <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" >http://somecom.com/SomeJavaRelyingParty</saml:Issuer> <saml:Subject xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"> <saml:NameID>harold_dt</saml:NameID> </saml:Subject> <saml:Action xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Namespace="urn:oasis:names:tc:SAML:1.0:action:rwedc">read</saml:Action> </samlp:AuthzDecisionQuery>