1.scala 处理xml
2. 获取属性
3.修改节点
4.遍历
5.模式匹配
6.命名空间
7.文件加载
import scala.xml._ /** * @author xwolf * @since 1.8 */ object Xml { def main(a:Array[String]):Unit={ //dubboXml() //createXml() loadXml() } def createXml():Unit={ //xml 定义 //直接写出xml ,scala 直接转为内嵌的Elem val xml = <xml version="1.0" encoding="UTF-8"> <appid>wx2421b1c4370ec43b</appid> <attach>支付测试</attach> <body>APP支付测试</body> <!-- 商户号信息--> <mch_id key="BJ">10000100</mch_id> <nonce_str strType="random">1add1a30ac87aa2db72f57a2375d8fec</nonce_str> <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url> <out_trade_no><![CDATA[1415659990]]></out_trade_no> <spbill_create_ip><![CDATA[14.23.150.211]]></spbill_create_ip> <total_fee>1</total_fee> <trade_type>APP</trade_type> <sign>0CB01533B8C1EF103065174F50BCA001</sign> </xml> println(xml) //获取指定节点 val mch_id = xml\"mch_id" println(mch_id) //取出内容 println(mch_id.text) //获取属性信息 val k= mch_id@"key" println(k) val vsn =xml.attribute("version").getOrElse() println(vsn) //将属性信息转化为map val ats = xml.attributes.asAttrMap println(ats) //查找元素信息 val no = xml.child.find(s => s.text.equals("1415659990")).get println(no) println( no.text) //修改属性 val s = xml % Attribute(null,"version","10.32",Null) println(s) println("********遍历*******") each(xml) } def each(p:Elem):Unit={ //遍历子节点信息 for(e <- p.child){ matchs(e) } } //模式匹配 def matchs (n:Node):Unit = { n match { case <li>{_}</li> => println("li:"+n) //文本匹配 case <total_fee>{Text(t)}</total_fee> => println("totalFee:"+t) case _ => println("未匹配:"+n) } } def dubboXml():Unit={ val config = <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-world-app" /> <dubbo:protocol name="jms"> <dubbo:parameter key="queue" value="http://10.20.160.198/wiki/display/dubbo/10.20.31.22" /> </dubbo:protocol> <dubbo:registry address="multicast://224.5.6.7:1234" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoServiceLocal" /> <dubbo:reference id="demoServiceRemote" interface="com.alibaba.dubbo.demo.DemoService" /> </beans> //获取命名空间 val namespace =config.namespace println(namespace) //获取指定前缀的命名空间 val dubbo=config.getNamespace("dubbo") println(dubbo) val registry = config"registry" println(registry) println(registry@"address") } def loadXml(): Unit ={ val xml=XML.loadFile("E:\tools\apache-tomcat-8.5.12_idea_1\conf\server.xml") println(xml) } }