Analytical configured to map xml

package com.javen.test;

import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

com.javen.TestXmlJdom amount;

{class PropertiesDemo public
/ **
* converted map xml file form, which has a key value for the node name, and all of its ancestor nodes are prefixed by
*. "" connected. Such as: SubscribeServiceReq.Send_Address.Address_Info.DeviceType
*
* @param xmlStr
* XML content
* @return Map map is converted to return
* /
public static the TreeMap <String, String> xml2Map (String xmlStr) throws JDOMException, IOException {
the TreeMap <String, String > = new new rtnMap the TreeMap <String, String> ();
SAXBuilder new new SAXBuilder Builder = ();
// the Document DOC = (the Document) builder.build (the StringReader new new (xmlStr));
the Document DOC = builder.build (TestXmlJdom.class .getClassLoader ()
.getResourceAsStream ( "Spring-mail.xml"));
// root node
Element root = doc.getRootElement ();
Root.getName rootName = String ();
rtnMap.put ( "root.name", rootName);
// recursive function calls, to obtain the names and values of all elements of the lowest level, the added map
convert (root, rtnMap, rootName) ;
rtnMap return;
}

public static String getName (String name) {
String value = "";
the try {
the TreeMap <String, String> xml2Map Map = ( "");
value = as map.get (name);
} the catch (Exception E) {
e.printStackTrace ();
}
return value;
}

/ **
* recursive function, identify the lowermost node and added to the map, called by xml2Map method.
*
* @Param E
* XML nodes, including the root node
* @param Map
* target Map
* @param LastName
* name of the connection from the root node to a node string
* /
@SuppressWarnings("rawtypes")
public static void convert(Element e, Map<String, String> map, String lastname) {
if (e.getAttributes().size() > 0) {
Iterator it_attr = e.getAttributes().iterator();
while (it_attr.hasNext()) {
Attribute attribute = (Attribute) it_attr.next();
String attrname = attribute.getName();
String attrvalue = e.getAttributeValue(attrname);
// map.put( attrname, attrvalue);
map.put(lastname + "." + attrname, attrvalue); // key 根据根节点 进行生成
}
}
List children = e.getChildren();
Iterator it = children.iterator();
while (it.hasNext()) {
Element child = (Element) it.next();
/ * String name = LastName + + child.getName () "."; * /
String name = child.getName ();
// if the child is a recursive call
if (child.getChildren () size () >. 0) {
Convert (child, Map, LastName + + child.getName ( "."));
} the else {
// If no child node, the value added put Map
map.put (name, child.getText ());
// If the node has attributes, attribute values put all joined Map
IF {(child.getAttributes () size ()> 0.)
the Iterator attr = child.getAttributes () Iterator ();.
the while (attr.hasNext ()) {
the attribute attribute = (the attribute) attr.next ();
String Attribute.getName attrName = ();
String attrvalue = child.getAttributeValue (attrName);
"." map.put (LastName child.getName + + () "." + + attrname, attrvalue );
}
}
}
}
}

public static void main(String args[]) throws JDOMException, IOException {
String str = "<xml><appid><a>aaaa</a><b>bbbbceshi</b></appid><attach>支付测试</attach><body>APP支付测试</body><mch_id>10000100</mch_id><nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>"
+ "<notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url><out_trade_no>1415659990</out_trade_no><spbill_create_ip>14.23.150.211</spbill_create_ip>"
+ "<total_fee>1</total_fee><trade_type>APP</trade_type><sign>0CB01533B8C1EF103065174F50BCA001</sign></xml>";

System.out.println(xml2Map(str));
}


}

Guess you like

Origin www.cnblogs.com/worfs/p/12348946.html