Writing a blog post in programming is purely a record of your own problems.
Because my language organization skills are poor, my friends who have read this article will read it.
The recent work involves the comparison of xml files to determine whether the data we have entered and the final calculation result are correct. Our system uses freemark to generate xml, so the node sequence can be set by ourselves, but it is often counterproductive. The node sequence of the standard xml file provided by the customer cannot guarantee that the node sequence is the same for each time. So I wrote a program to re-assemble the xml file.
First, it is easier to understand:
It may be like this before sorting: standerdXml on the right and selfXml on the left
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1551444P1-0.jpg "title =" 1.jpg"/>
After reorganization: newMml on the left and standerXml on the right
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1551443O5-1.jpg "title =" 2.jpg"/>
The following code is used to record your internship ideas:
Xml files are divided into three types:
The xml file provided by the customer.StandardXmlReplace;
The xml file generated by us.SelfXmlReplace;
The xml file generated by the program and written to the file system.NewXml.
Configuration information, mainlyStandardXmlAndSelfXmlFolder and namespace in xml
Static final String standardForderPath = "D :\\ stand"; // static final String selfForderPath = "D :\\ self "; // static final String [] namespaceForPrefix = {"xsi", "HIP", "SAP", "SAP09", "CS "}; // All namespaces used
Since restructuring is required, we must first compareStandardXmlAnd then use xPath To Go To The selfXml file to check whether the Node also exists. If so, all the information attributes and values of the node are obtained from selfXml) take it out and put it in newXml, and so on, so that an xml file with the same sequence as the standardXml node will come out.
Put the xml in the two folders into two sets, and then use the following method to retrieve the two copies with the same name and put them in the array to return.
/*** Two xml files are provided each time to compare the xml obtained from * xmls [0] as the standard folder, and * xmls [1] is the xml file to be compared. */Public File [] provideXML (List <File> standaredList, List <File> selfsList) {File [] xmls = new File [2]; for (File standaredXML: standaredList) {String tempName = standaredXML. getName (); int findNum = 0; for (int I = 0; I <selfsList. size (); I ++) {if (tempName. equals (selfsList. get (I ). getName () {File selfXML = selfsList. get (I); xmls [0] = standaredXML; xmls [1] = selfXML; standaredList. remove (standaredXML); selfsList. remove (selfXML); findNum ++; return xmls ;}}if (findNum = 0) {standaredList. remove (standaredXML); return null ;}
The following method is the core for element restructuring:
Public HashMap <Integer, Object> loopAndCreateXml (HashMap <Integer, Object> map) {if (newDoc = null) {newDoc = this. createDocument ();} Element standerdXmlElement = (Element) map. get (1); xpathSelector = incluenthelper. createXPath (standerdXmlElement. getUniquePath (); Element e = (Element) xpathSelector. selectSingleNode (Document) map. get (2); if (e! = Null) {System. out. println ("extracted node name:" + e. getQualifiedName (); if (e. getParent ()! = Null) {xpathSelector = incluenthelper. createXPath (e. getParent (). getUniquePath (); fatherElement = (Element) xpathSelector. selectSingleNode (newDoc);} if (fatherElement = null) {fatherElement = newDoc. addElement (e. getQualifiedName (), e. getNamespaceURI (); fatherElement. setAttributes (e. attributes (); String spaceUri = ""; Element rootElt = (Document) map. get (2 )). getRootElement (); // get the root node String defN Amespace = rootElt. getNamespaceURI (); // process the namespace if (defNamespace! = Null) {nameSpaceMap. put ("defu", defNamespace) ;}for (String nameSpace: Paths. namespaceForPrefix) {spaceUri = rootElt. getNamespaceForPrefix (nameSpace )! = Null? RootElt. getNamespaceForPrefix (nameSpace). getURI (): null; if (spaceUri! = Null) {nameSpaceMap. put (nameSpace, spaceUri); fatherElement. addNamespace (nameSpace, spaceUri) ;}} else {fatherElement = fatherElement. addElement (e. getQualifiedName (), e. getNamespaceURI (); fatherElement. setText (e. getText (); fatherElement. setAttributes (e. attributes ());}}
The source code has been uploaded. Anyone who wants to know can go to the source code. No matter what code is written, it still brings convenience to my work, and only in this way can I truly experience the fun of programming.
QQ group: 105901697 (computer enthusiasts), 112456134 (Task Group) Welcome to join
This article is from the "Thinking-Programming" blog, please be sure to keep this source http://zp1990.blog.51cto.com/2267648/1289516