Java_ Data Interchange _dom4j_01_ parsing XML

Source: Internet
Author: User

1. Description

The detailed principle is studied later, the example is saved first.

2. Code 2.1 XML content
<?XML version= "1.0" encoding= "UTF-8"?><Weixin:qi-configxmlns:weixin= "Aurora.plugin.weixin">    <instnces>                <weixin:qiyeancetoken= "Wei wins more negative less Xin"Corpid= Defensive play B5a4889c1e12 "Encodingaeskey= "Z3s7tgh3mknc2 Rice cooker W5MEVWYOCGMEL1GTW"AppName= "rule issued to C"/>    </instnces>    <Tokentasks>    <Weixin:qiyaskGroupName= "HEC"Corpid= "Wwa4889c1e12"Secrect= "Kopzjwdsmdhghob7fydffw6m0-ssmoqyv_cko" />    </Tokentasks></Weixin:qi-config>
View Code

2.2java Code
 Packagecom.ray.test;ImportJava.io.File;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.dom4j.Attribute;Importorg.dom4j.Document;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;Importorg.junit.Test; Public classTestparsexml {Map<string, map<string,string>> weixinconfigmap=NewHashmap<string, map<string,string>>(); /*** Gets the Document object for the file and then gets the corresponding root node *@throwsException*/@Test Public voidTestgetroot ()throwsexception{saxreader sax=NewSaxreader ();//Create a Saxreader object//file Xmlfile=new file ("Src/main/java/aurora/plugin/weixin/qiyewexin.config");//creates a file object based on the specified pathFile xmlfile=NewFile ("Src/com/ray/test/qiyewexin.config");//creates a file object based on the specified pathDocument Document=sax.read (xmlfile);//gets the document object, which throws a exception early end if there are no nodesElement root=document.getrootelement ();//Get root node         This. getnodes (Root);//traverse all nodes starting from the root node                 for(String key:weiXinConfigMap.keySet ()) {System.out.println ("Node Name: Key" +key); Map<string,string> attmap=Weixinconfigmap.get (key);  for(String key2:attMap.keySet ()) {System.out.println ("Property name:" +key2+ ", Property value:" +Attmap.get (Key2)); }        }    }      /*** Recursively traverse all child nodes starting from the specified node *@paramnode*/     Public voidGetnodes (Element node) {System.out.println ("--------------------"); //the name, text content, and properties of the current nodeSystem.out.println ("Current node Name:" +node.getname ());//Current node nameSystem.out.println ("Contents of current node:" +node.gettexttrim ());//Current node nameList<attribute> listattr=node.attributes ();//List of all properties of the current nodeMap<string,string> attrmap=NewHashmap<string,string>();  for(Attribute attr:listattr) {//Traverse all properties of the current nodeString Name=attr.getname ();//Property nameString Value=attr.getvalue ();//the value of the propertySystem.out.println ("Property name:" +name+ "Property value:" +value); //storing configuration properties in map            if("Qiyeweixin-instance". Equals (Node.getname ()) | | Qiyetoken-task ". Equals (Node.getname ()))            {attrmap.put (name, value); }        }          //recursively traverse all child nodes of the current nodeList<element> listelement=node.elements ();//List of all first-level child nodes         for(Element e:listelement) {//Traverse all first-level child nodes             This. Getnodes (e);//Recursive        }          if("Qiyeweixin-instance". Equals (Node.getname ()) | | Qiyetoken-task ". Equals (Node.getname ()))        {Weixinconfigmap.put (Node.getname (), attrmap); }    }      /*** Recursively traverse all child nodes starting from the specified node *@paramnode*/     Public voidGetNodes0 (Element node) {System.out.println ("--------------------"); //the name, text content, and properties of the current nodeSystem.out.println ("Current node Name:" +node.getname ());//Current node nameSystem.out.println ("Contents of current node:" +node.gettexttrim ());//Current node nameList<attribute> listattr=node.attributes ();//List of all properties of the current node         for(Attribute attr:listattr) {//Traverse all properties of the current nodeString Name=attr.getname ();//Property nameString Value=attr.getvalue ();//the value of the propertySystem.out.println ("Property name:" +name+ "Property value:" +value); }          //recursively traverse all child nodes of the current nodeList<element> listelement=node.elements ();//List of all first-level child nodes         for(Element e:listelement) {//Traverse all first-level child nodes             This. Getnodes (e);//Recursive        }      }  }
View Code

Java_ Data Interchange _dom4j_01_ parsing XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.