TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:6 Z6 L) z7 ~, k0 i
$ l1 [# E# n. m: B: J- <?xml version="1.0" encoding="UTF-8"?>8 {7 r1 B5 @7 o! w$ v3 A% j$ G+ V
- <result>9 \: U; j8 U& @/ H/ V/ A
- <question>. x7 h- s' f u# t% H
- <code>001</code>
$ r2 u& F: J0 p - <name>张三</name># A3 w$ m4 s: c8 ]& v8 S
- </question>( j. w2 V o* y: N, W% i
- <question>
7 D* @2 C4 T+ F3 v - <code>002</code>7 b+ c& R+ Q! ^1 p! l. N( h b
- <name>李四</name>* j d/ i* f5 \
- </question>
@' y6 v$ ^7 D4 h# M3 n - <question>
; y% Z/ A0 k* C5 ~% j! H1 _. G - <code>003</code>
. r R0 ?* h$ w7 ~ y - <name>王二</name>' K8 J( s7 |" [2 d; ]
- </question>* s0 c" ]" ^! W6 ~% J
- <question>3 o! g! J' \" f% ^) O/ o* ?) O1 K
- <code>004</code>
/ h# S- x0 T# Z) L# H* U0 p - <name>麻子</name>& o4 @8 n% `) l, `
- </question>
1 i, G6 y( ? x* y& G2 M# s2 x- I - <question>* i" L+ ^- ]3 H& y$ S* F) T
- <code>005</code>: N6 T j( U" y y6 `+ f o
- <name>科帮网</name> J9 Y1 [" [3 F8 ^3 L, f; g; D6 {
- </question>
. b% T8 l% O7 {3 @. ~, k - </result>
复制代码 XmlReadUtil.java:读取方法
. u9 P6 s( d& H% z' W- package com.itstyle.util;* i r1 l; a/ `, ~1 R r6 j
- ! G7 `1 h+ {; J; _' |* w3 u/ }
- import java.io.File;
3 m+ v+ a0 k6 {0 D) B3 h8 J& \ - import java.util.Iterator;* y- S* }: }. [4 `+ B$ ~
- import java.util.Map;
/ m# p3 A" D) D' ? - $ H$ l' b8 e- ^. v, {* E6 X
- import org.dom4j.Attribute;
7 G# j, H2 e* z. v - import org.dom4j.Document;
/ e& B4 M2 o& D# N - import org.dom4j.Element;- ^- T1 J" {; D) C9 K9 |. D; P+ b
- import org.dom4j.io.SAXReader;
; X- |) D: d* L) O/ m- [ - /**
" X4 X4 @3 U- d3 s+ ? - *
& q7 N$ _: P) } - * @author 科帮网(www.52itstyle.com)+ ~6 Z* @' X0 w: }3 m
- *
* g4 p3 J, n7 G. w4 c - */% `" a- c% k3 a) i2 C
- public class XmlReadUtil {% U1 i q- L6 q, t2 H" b( P; o
- @SuppressWarnings("rawtypes")
x9 W" K- d0 m4 A7 V& V, b - public static void listBaseInfo(String filePath, Map<String, String> map) {7 E' D) j- d! S! f
- SAXReader saxReader = new SAXReader();1 ^) h+ Y5 H0 a4 ]1 ]8 |9 E# G" c
- try {; c" y) T. s1 u/ `* Z1 [
- Document document = saxReader.read(new File(filePath));
5 ]. m' T8 V& t, |/ h2 ^ - Element root = document.getRootElement();
, M+ c) [% m9 ^+ g - // 用于记录信息编号的变量
1 E4 C, Z$ q0 B N. D% ] - int num = -1;, k" s+ X; S; K' R; A( E* Q X
- // 遍历根结点(result)的所有孩子节点(肯定是question节点)
& F5 M$ G Q7 \2 M( G* C: o/ x - Attribute attrCode = root.attribute("code");
& _: p# T5 j! g' {% G% M/ I - Attribute attrName = root.attribute("name");
, n4 ?) s+ S e: p' M; p9 x - if(attrCode!=null){/ I# A1 H a0 w+ d' y( v. V
- map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
5 g& J$ v2 i; ?1 _+ A* ? - }
; q9 e4 Z* ^* K) G - if(attrName!=null){* I: U5 l$ L' B3 K+ I/ p) h; T
- map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());, [% z- f0 _0 z, b
- }9 x+ E7 g3 V: s
- for (Iterator iter = root.elementIterator(); iter.hasNext();) {
9 X! W- I* d1 {0 M - Element element = (Element) iter.next();
$ @* X z3 c1 T) B6 Q7 b1 }7 h. O - num++;9 G# h; v1 v! A9 i- X
- // 遍历question结点的所有孩子节点(即code, name),并进行处理" C$ P$ y* [" R/ [ J+ ~0 G6 \
- for (Iterator iterInner = element.elementIterator(); iterInner
6 z" z0 b8 P& m - .hasNext();) {$ p1 T* E8 C" `; R
- Element elementInner = (Element) iterInner.next();; d, V O1 X" B
- map.put(elementInner.getName() + num,# {& J2 ^5 |, a( B6 @
- elementInner.getText());
* H. b2 `+ }" l/ a2 L3 _ - }
/ J' \& e( h) D: p - }
; _, m+ \$ x! N: j
, I2 E8 q! L3 w8 D9 s- } catch (Exception e) {
7 U" ~+ S. K1 d- g - e.printStackTrace();! v0 \4 L! g- a- A2 ]/ z
- }& k9 `2 @! k9 a" u4 D4 P
- ! i; ]7 F, @. V+ e3 V
- }5 w; R: h! q: P7 q5 ?
- }
1 y! G8 x& X' q
复制代码 ReadXmlServlet.java:4 c: D. D$ M d+ ?( l
- package com.itstyle.servlet;
( A( ^ y5 P9 ^' D! b2 ~2 Q - 4 M' y1 W M: ]# [9 a
- import java.io.IOException;
9 U+ t+ Q6 U) L( o - import java.io.PrintWriter;- T6 n( D, X# C, D9 q6 s( G/ {0 A
- import java.util.ArrayList;
1 Y3 z7 w* U5 [! Y: f' M' i - import java.util.List; }% R% ~/ {/ @$ k f0 R0 H. h
, k+ u' s& y4 M) {$ _9 l! A- import javax.servlet.ServletException;
6 ~9 Q* g y8 z/ ?) {) g: c - import javax.servlet.http.HttpServlet;: I+ N! U- p' N* R' s' Q! m p4 I9 O( S
- import javax.servlet.http.HttpServletRequest;4 {# k6 J/ V# z0 A% U7 N
- import javax.servlet.http.HttpServletResponse;( i: {# \" z6 c- p; j
- - m/ m9 X) [. b
- import net.sf.json.JSONArray;( X" N. P- t+ Y
- import net.sf.json.JSONObject;
# l5 [, _7 }7 i$ N. J8 N
! r# ?, Q. s; E( g* V- import com.itstyle.model.CommonEntity;; m8 M3 U$ K( Q4 E( X, p; |
- import com.itstyle.util.PropertiesListUtil; a" }! ^* L/ d6 r; p2 m
- /**
1 S3 o/ n$ b9 j; ?" ^2 A. o0 w - * : u1 \" v8 Q, z- f$ k
- * @author 科帮网(www.52itstyle.com)$ f( _0 v" b4 ?$ r# f- M
- *
y7 |* Q3 U+ w, A( F) k - */, E* U9 d1 u& C g$ `* ^
- public class ReadXmlServlet extends HttpServlet {* o5 f0 h6 w8 ^, N5 O) m
- 7 \4 b1 ?/ Y) d* s# }
- private static final long serialVersionUID = 1L;
- J0 v) |! s& P) b1 l - private List<CommonEntity> listStu = new ArrayList<CommonEntity>();9 Z# p! {4 `7 p* o; p8 U, O2 P
- public void init() throws ServletException {
" W9 W5 l% q A - // 初始化阶段xml( o) t2 D S1 `, }# S$ l' h
- String path = getServletContext().getRealPath("/file/stuName.xml");
9 V K" _; L P8 w; n6 y - try {" o5 g: O5 q* ]
- listStu = PropertiesListUtil.listXml(path);7 x2 x: h; ?9 R; F3 ~: S
- System.out.println(listStu.size());
2 h% m: u1 S5 X: \5 l9 z - } catch (Exception e) {( v7 K# d8 _) e; _5 S
- e.printStackTrace();! R' A" ^$ d. a6 N
- }
8 D' u7 }% m; Y W9 Z - }( J1 H( `' o1 c" K o4 E% B6 |8 `
- public void doGet(HttpServletRequest request, HttpServletResponse response)
& r' U' q* A! x7 [. T) s - throws ServletException, IOException {
1 |! l% b, X( k& v - JSONArray array = new JSONArray();/ E+ E: [$ |8 P$ E! R
- response.setCharacterEncoding("UTF-8");
/ H+ B( |* D; J - PrintWriter out = response.getWriter();( I3 r j- M8 z6 g3 F
- try {
! j! t. D; j4 T% { - for(CommonEntity entity:listStu){
4 _& N) j7 X) b9 j9 G - JSONObject obj = new JSONObject();
# h4 l0 A! I- \0 z - obj.put("code", entity.getEntityCode());" v6 i( @% b8 @8 t& X! O8 F
- obj.put("name", entity.getEntityName());
! v3 F$ v% M g - array.add(obj);
5 L" j- M' W7 h. n4 C1 y; M, X& z - }
: j% \6 T: @% q, O9 f% c E - out.print(array.toString());
) P$ v. D% v" p- a) J5 s - }catch (Exception e) {
) e. |; B3 M& U# n2 H$ E ] - e.printStackTrace();
6 V8 f9 g# F0 V0 W. C; {; e, j/ F - } finally {2 |8 P$ Y$ ?5 s# H/ H: S
- out.close();
, j6 ~- O' P% M* S: \) g - }- _5 x C. j7 @! N6 `1 J; m
- }
. m9 e1 w6 r: u1 y
9 f. m# x. `. U* Z! ?6 ^6 J- public void doPost(HttpServletRequest request, HttpServletResponse response)2 @; m$ O1 _1 D- X1 y! b. H! J
- throws ServletException, IOException {: i& a ^+ U! H
- doGet(request, response);8 r, L6 ~( K2 E# m! U% h" ]
- }! m; k5 z n9 o/ t2 z
& v2 m# v) i. E9 g) s3 F- }- n1 i! |& b3 z4 l
复制代码
! E: j8 |$ j& W8 E7 j; ejava使用dom4j解析xml并展示demo(科帮网)下载:点击下载# `" `% R9 u6 k4 n0 \" p* ?, I; {; G
# Y9 H a! r! A4 W2 s( }3 N
/ u, r8 y' c1 i
' w3 ]1 R2 ]; K1 z
% O" ?0 v) T$ ]3 f* Z3 f- p% D6 ~ |
|