让我们了解下DocumentBuilderFactory------------【spring源码】

DocumentBuilderFactory,它的详情在这张图片上。。。

 

 

从上面我们了解到这个包是,提供允许处理XML文档的类。而且我们分析的类是:定义工厂API,使应用程序能够从XML文档中获取生成DOM对象树的解析器。

 

如果不明白和应用的话,可以看下这篇文章:DocumentBuilderFactory解析XML

 

 

至于其中比较重要的方法:newInstance方法,它会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。

但是在DocumentBuilderFactory类中newInstance方法有两个,但它们方法名一样,方法签名不一样的。

 

第一个,

    public static DocumentBuilderFactory newInstance() {
        return FactoryFinder.find(
                /* The default property name according to the JAXP spec */
                DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
                /* The fallback implementation class name */
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
    }

 

第二个,

    public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader         
        classLoader){
            //do not fallback if given classloader can't find the class, throw exception
            return FactoryFinder.newInstance(DocumentBuilderFactory.class,
                        factoryClassName, classLoader, false);
    }

 

至于为什么的话,就是在有名字和类加载器的使用用自定义的,没有的话,就是用默认的呗。其实一般都会这样。。。

 

我们了解到这里的话,也就差不多了。

至于它的用处的话,就是它会作用在哪里,就对应着它的功能。

 

所以在spring源码中,它用在了获取 Document 对象的过程中。

贴下图片,

 

详细代码:

至于自定义,我相信你能猜到的。。。

 

 

要是哪里写的地方不对,还望多多指教。

参考资料:

https://docs.oracle.com/javase/8/docs/api/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用任何一种XML到JSON转换工具来将Spring Cloud Gateway的连接类型XML转换为JSON格式。以下是一个使用Java的例子: ```java import org.json.JSONObject; import org.w3c.dom.Document; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; public class XmlToJsonConverter { public static void main(String[] args) throws Exception { String xml = "<Connection>\n" + " <HttpClient/>\n" + " <Proxy/>\n" + "</Connection>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); JSONObject json = new JSONObject(); json.put(doc.getDocumentElement().getNodeName(), traverse(doc.getDocumentElement())); System.out.println(json.toString()); } private static JSONObject traverse(org.w3c.dom.Element element) { JSONObject obj = new JSONObject(); org.w3c.dom.NodeList nodeList = element.getChildNodes(); if (nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { org.w3c.dom.Node node = nodeList.item(i); if (node instanceof org.w3c.dom.Element) { org.w3c.dom.Element child = (org.w3c.dom.Element) node; if (child.getChildNodes().getLength() == 1) { obj.put(child.getNodeName(), child.getTextContent()); } else { obj.put(child.getNodeName(), traverse(child)); } } } } return obj; } } ``` 这个代码将会把你提供的XML转换成以下的JSON格式: ```json { "Connection": { "HttpClient": {}, "Proxy": {} } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值