• w3c document中节点名字的简单替换


    简单的替换操作,基本不需引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>
  • 相关阅读:
    Http学习(一)
    Android Studio 创建aar包与引用
    C语言模块化编译介绍
    程序结构和分支语句介绍
    数据类型、常量、变量、printf、scanf和运算符
    第一个C语言程序
    Xcode相关整理
    Java: IO 学习小结
    Java: RandomAccessFile
    Java: IO 字符流
  • 原文地址:https://www.cnblogs.com/xzs603/p/2857285.html
Copyright © 2020-2023  润新知