mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	增加xml解析工具类
This commit is contained in:
		| @ -114,6 +114,11 @@ | ||||
|       <artifactId>assertj-guava</artifactId> | ||||
|       <scope>test</scope> | ||||
|     </dependency> | ||||
|     <dependency> | ||||
|       <groupId>org.dom4j</groupId> | ||||
|       <artifactId>dom4j</artifactId> | ||||
|       <version>2.0.0</version> | ||||
|     </dependency> | ||||
|   </dependencies> | ||||
|  | ||||
|   <build> | ||||
|  | ||||
| @ -0,0 +1,96 @@ | ||||
| package me.chanjar.weixin.common.util; | ||||
|  | ||||
| import java.io.StringReader; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| import org.dom4j.Document; | ||||
| import org.dom4j.DocumentException; | ||||
| import org.dom4j.Element; | ||||
| import org.dom4j.Node; | ||||
| import org.dom4j.io.SAXReader; | ||||
| import org.dom4j.tree.DefaultText; | ||||
|  | ||||
| import com.google.common.collect.Lists; | ||||
| import com.google.common.collect.Maps; | ||||
| import com.google.common.collect.Sets; | ||||
|  | ||||
| /** | ||||
|  * <pre> | ||||
|  * XML转换工具类. | ||||
|  * Created by Binary Wang on 2018/11/4. | ||||
|  * </pre> | ||||
|  * | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  */ | ||||
| public class XmlUtils { | ||||
|  | ||||
|   public static Map<String, Object> xml2Map(String xmlString) { | ||||
|     Map<String, Object> map = new HashMap<>(16); | ||||
|     try { | ||||
|       SAXReader saxReader = new SAXReader(); | ||||
|       Document doc = saxReader.read(new StringReader(xmlString)); | ||||
|       Element root = doc.getRootElement(); | ||||
|       List<Element> elements = root.elements(); | ||||
|       for (Element element : elements) { | ||||
|           map.put(element.getName(), element2MapOrString(element)); | ||||
|       } | ||||
|     } catch (DocumentException e) { | ||||
|       throw new RuntimeException(e); | ||||
|     } | ||||
|  | ||||
|     return map; | ||||
|   } | ||||
|  | ||||
|   private static Object element2MapOrString(Element element) { | ||||
|     Map<String, Object> result = Maps.newHashMap(); | ||||
|  | ||||
|     final List<Node> content = element.content(); | ||||
|     if (content.size() <= 1) { | ||||
|       return element.getText(); | ||||
|     } | ||||
|  | ||||
|     final Set<String> names = names(content); | ||||
|     if (names.size() == 1) { | ||||
|       // 说明是个列表,各个子对象是相同的name | ||||
|       List<Object> list = Lists.newArrayList(); | ||||
|       for (Node node : content) { | ||||
|         if (node instanceof DefaultText) { | ||||
|           continue; | ||||
|         } | ||||
|  | ||||
|         if (node instanceof Element) { | ||||
|           list.add(element2MapOrString((Element) node)); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       result.put(names.iterator().next(), list); | ||||
|     } else { | ||||
|       for (Node node : content) { | ||||
|         if (node instanceof DefaultText) { | ||||
|           continue; | ||||
|         } | ||||
|  | ||||
|         if (node instanceof Element) { | ||||
|           result.put(node.getName(), element2MapOrString((Element) node)); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   private static Set<String> names(List<Node> nodes) { | ||||
|     Set<String> names = Sets.newHashSet(); | ||||
|     for (Node node : nodes) { | ||||
|       if (node instanceof DefaultText) { | ||||
|         continue; | ||||
|       } | ||||
|       names.add(node.getName()); | ||||
|     } | ||||
|  | ||||
|     return names; | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,78 @@ | ||||
| package me.chanjar.weixin.common.util; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import org.testng.annotations.*; | ||||
|  | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
|  | ||||
| /** | ||||
|  * <pre> | ||||
|  * | ||||
|  * Created by Binary Wang on 2018/11/4. | ||||
|  * </pre> | ||||
|  * | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  */ | ||||
| public class XmlUtilsTest { | ||||
|  | ||||
|   @Test | ||||
|   public void testXml2Map() { | ||||
|     String xml = "<xml>\n" + | ||||
|       "<CopyrightCheckResult>\n" + | ||||
|       "<Count>2</Count>\n" + | ||||
|       "<ResultList>\n" + | ||||
|       "<item>\n" + | ||||
|       "<ArticleIdx>1</ArticleIdx>\n" + | ||||
|       "<UserDeclareState>0</UserDeclareState>\n" + | ||||
|       "<AuditState>2</AuditState>\n" + | ||||
|       "<OriginalArticleUrl><![CDATA[Url_1]]></OriginalArticleUrl>\n" + | ||||
|       "<OriginalArticleType>1</OriginalArticleType>\n" + | ||||
|       "<CanReprint>1</CanReprint>\n" + | ||||
|       "<NeedReplaceContent>1</NeedReplaceContent>\n" + | ||||
|       "<NeedShowReprintSource>1</NeedShowReprintSource>\n" + | ||||
|       "</item>\n" + | ||||
|       "<item>\n" + | ||||
|       "<ArticleIdx>2</ArticleIdx>\n" + | ||||
|       "<UserDeclareState>0</UserDeclareState>\n" + | ||||
|       "<AuditState>2</AuditState>\n" + | ||||
|       "<OriginalArticleUrl><![CDATA[Url_2]]></OriginalArticleUrl>\n" + | ||||
|       "<OriginalArticleType>1</OriginalArticleType>\n" + | ||||
|       "<CanReprint>1</CanReprint>\n" + | ||||
|       "<NeedReplaceContent>1</NeedReplaceContent>\n" + | ||||
|       "<NeedShowReprintSource>1</NeedShowReprintSource>\n" + | ||||
|       "</item>\n" + | ||||
|       "</ResultList>\n" + | ||||
|       "<CheckState>2</CheckState>\n" + | ||||
|       "</CopyrightCheckResult>\n" + | ||||
|       "</xml>"; | ||||
|  | ||||
|     final Map<String, Object> map = XmlUtils.xml2Map(xml); | ||||
|     assertThat(map).isNotNull(); | ||||
|     final Map<String, Object> copyrightCheckResult = (Map<String, Object>) map.get("CopyrightCheckResult"); | ||||
|     List<Map<String, Object>> resultList = (List<Map<String, Object>>) ((Map<String, Object>) copyrightCheckResult.get("ResultList")).get("item"); | ||||
|     assertThat(copyrightCheckResult).isNotNull(); | ||||
|  | ||||
|     assertThat(copyrightCheckResult.get("Count")).isEqualTo("2"); | ||||
|     assertThat(copyrightCheckResult.get("CheckState")).isEqualTo("2"); | ||||
|  | ||||
|     assertThat(resultList.get(0).get("ArticleIdx")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(0).get("UserDeclareState")).isEqualTo("0"); | ||||
|     assertThat(resultList.get(0).get("AuditState")).isEqualTo("2"); | ||||
|     assertThat(resultList.get(0).get("OriginalArticleUrl")).isEqualTo("Url_1"); | ||||
|     assertThat(resultList.get(0).get("OriginalArticleType")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(0).get("CanReprint")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(0).get("NeedReplaceContent")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(0).get("NeedShowReprintSource")).isEqualTo("1"); | ||||
|  | ||||
|     assertThat(resultList.get(1).get("ArticleIdx")).isEqualTo("2"); | ||||
|     assertThat(resultList.get(1).get("UserDeclareState")).isEqualTo("0"); | ||||
|     assertThat(resultList.get(1).get("AuditState")).isEqualTo("2"); | ||||
|     assertThat(resultList.get(1).get("OriginalArticleUrl")).isEqualTo("Url_2"); | ||||
|     assertThat(resultList.get(1).get("OriginalArticleType")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(1).get("CanReprint")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(1).get("NeedReplaceContent")).isEqualTo("1"); | ||||
|     assertThat(resultList.get(1).get("NeedShowReprintSource")).isEqualTo("1"); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang