TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:# N/ [4 S% V& o) l |1 l
. h" O' ~* J) E* J- <?xml version="1.0" encoding="UTF-8"?>
" D4 s" H2 a/ _: ^$ R - <result>$ N$ e3 q9 h# F8 d
- <question>
3 ^' S9 O. n: X9 W - <code>001</code>
- j. M2 i' ~/ r" @ - <name>张三</name>! H! x3 L8 [6 W+ u& i" S
- </question>
1 D. M1 b* [2 R0 b# R" N( ]$ W. @ - <question>
0 A4 ^; M! L! b) b; n h4 [ - <code>002</code>2 p* e5 F: H- x7 a3 Q2 m3 A! o1 |
- <name>李四</name>
* ~; h1 |9 Z0 o# c - </question>
0 L+ J0 |/ B* i' A! l2 L" v2 ^( z - <question>6 Z E' \- g$ t+ _
- <code>003</code>0 T! `7 ^4 |& i& s4 a# ^% b+ Y
- <name>王二</name>
0 a2 Q+ I$ `& ^( q' o - </question>4 Z3 G+ Z5 @4 \4 z4 ]: B% _* B
- <question>1 T% ^* r3 f! ~6 e" `5 O( ] |8 q
- <code>004</code>
. z) [& C. p* q6 a n, F( z# R - <name>麻子</name>
2 B% G0 ^. D! k9 q& Q3 F+ m - </question>
' e3 V( v2 @' r6 R - <question>4 i4 i+ N6 c! E) } W. }6 p4 T" m
- <code>005</code>) ^' N9 C9 K/ c( S2 D3 Q5 W1 C
- <name>科帮网</name>$ P; Q, | a- O2 O) n
- </question>5 j: i, ~. D1 S+ ], j3 ^; K
- </result>
复制代码 XmlReadUtil.java:读取方法
7 a. J! ?1 z! X" ^8 u- ~; T- package com.itstyle.util;' l: T1 C6 ] N: a) D$ V/ f$ a
7 @9 v* H; N6 C0 i- import java.io.File;
9 g4 Z- ?# B5 M! }2 u3 p" k, H - import java.util.Iterator;: P) x* [$ r j8 G; b* ~
- import java.util.Map;
# Y$ g4 q. Z, j, p/ c2 m/ G - - b) E9 u: ?2 A! W4 y5 J
- import org.dom4j.Attribute;
5 q3 M, I7 J3 r: o% W - import org.dom4j.Document;
) y+ t6 V* V2 ~( Q* p" C - import org.dom4j.Element;: H4 J9 b% Z; X3 H% N4 E
- import org.dom4j.io.SAXReader;5 ]7 c' v" j1 I6 w2 e2 Z% H/ z4 `
- /**" t0 d1 j) }7 G7 q3 b7 |
- * 5 W+ B4 o' x- ?
- * @author 科帮网(www.52itstyle.com)
* o4 V# ~& t7 A6 @ - *
9 W9 ^$ [9 _5 R9 V: c: P - */* \. T9 b; ]3 _, |; i" J! {. ]
- public class XmlReadUtil {
, R2 _1 x L4 @7 ]+ C - @SuppressWarnings("rawtypes")0 v& _, \- N( w Z3 E
- public static void listBaseInfo(String filePath, Map<String, String> map) {
5 S, l8 s9 t9 r# { - SAXReader saxReader = new SAXReader();
/ Q- K8 O1 A% j% q' x - try {
+ {& \$ `: r) {* m: a - Document document = saxReader.read(new File(filePath));9 n/ ~7 ]; r. }6 U( d
- Element root = document.getRootElement();5 n4 \6 _5 V/ b
- // 用于记录信息编号的变量
* D3 \7 Z U4 C9 A; c. [ - int num = -1;
% r% m. p5 d9 d - // 遍历根结点(result)的所有孩子节点(肯定是question节点)1 u3 g* @9 G1 F4 b$ ~1 X
- Attribute attrCode = root.attribute("code");
! P d! H& E; g3 {$ G8 p( n - Attribute attrName = root.attribute("name");" i' P6 {( p$ p2 W. m% j
- if(attrCode!=null){% _3 \# `) ?: X) q$ ]- H
- map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
# y7 G8 ^. O/ }( s, l- U( o - }
9 U" `/ x/ s$ X0 a - if(attrName!=null){" Q o# t% J! t1 ^
- map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());$ C8 [2 n2 @! Y
- }6 ^/ `3 L) U/ M4 t; F+ j
- for (Iterator iter = root.elementIterator(); iter.hasNext();) {0 y! T7 C; [: ^0 v
- Element element = (Element) iter.next();3 A$ }' R# n* q: n* K. {4 z
- num++;
[; ~9 J5 |0 m - // 遍历question结点的所有孩子节点(即code, name),并进行处理
p4 o, x9 f, ]$ z' | - for (Iterator iterInner = element.elementIterator(); iterInner
8 |0 ?. @8 e* Z# P& n% G - .hasNext();) {1 T$ z$ g# }. {* ^+ ~7 C
- Element elementInner = (Element) iterInner.next();
2 W! @# ~- o J8 r3 c! Q - map.put(elementInner.getName() + num,) L- O8 w6 W) H$ R9 x. i! F; N
- elementInner.getText());8 D9 f4 \4 g/ g# W6 i
- }
0 L7 |# ]$ `' M# C; B( j' N. t - }
6 o4 O% z- m+ ~" i- Y# q: O) t, g, n - / T* X( T E8 I t& z. S" N
- } catch (Exception e) {. l8 |6 O; x( d$ R+ R! R9 u0 B
- e.printStackTrace();
* g; t7 S& z' |( o% p - }* z; q- k/ c( L0 _, d/ Y( }" |
9 U, b) G7 `% a9 P; b' u$ A: w- }
6 ? E( _" k2 `5 A3 g - }8 C' ~) j# n0 m9 x* A
复制代码 ReadXmlServlet.java:
' g2 T; T/ x* C- C9 Y# D) s- package com.itstyle.servlet;% ~# ?1 }( N$ ^* }( j( o! Z
- ( ?$ c% L' a1 M. L
- import java.io.IOException;
, q5 V) T( q1 V: D - import java.io.PrintWriter;
1 y7 ?. j* ?+ R0 t2 x - import java.util.ArrayList;; H6 z! D9 s6 J+ Q/ Y" f
- import java.util.List;
, g! I7 a6 h8 } o1 B, C1 C
; t* [4 p: j2 F; E( A( H, F: |2 v- import javax.servlet.ServletException;
, h! e: i i( b b - import javax.servlet.http.HttpServlet;; H9 q n8 p& t! c7 N9 p
- import javax.servlet.http.HttpServletRequest;
. B. N7 a, h" @! l+ V' } - import javax.servlet.http.HttpServletResponse;5 Z1 W: u4 R0 H' P$ c$ ] c
# K/ H% W1 `* C. \- m2 E- B- import net.sf.json.JSONArray;
* v5 W5 ?9 X- _6 v" R3 C - import net.sf.json.JSONObject;2 T. b6 D- U& Y, k4 L, U
/ k# }# ?8 Q1 G h- import com.itstyle.model.CommonEntity;0 L, Z k Q8 y2 i5 S" W+ q$ A
- import com.itstyle.util.PropertiesListUtil;
D3 u* F) v4 L0 V - /**
- M2 ^3 O+ @/ J8 i0 ~+ ]( X; ` - *
6 K. g' k* i, a - * @author 科帮网(www.52itstyle.com), c5 W% I- d, g" r' w/ o
- *; ~! T# L/ M( e+ l, i' d" i
- */
# n$ h& D% j8 T) D: u6 U - public class ReadXmlServlet extends HttpServlet {
- p0 q! z& x( Z; G L# p+ q - ) E$ B3 Y! M0 B# Z2 `) ]; U
- private static final long serialVersionUID = 1L;
: D1 q. m. I- n; I! M& b$ O - private List<CommonEntity> listStu = new ArrayList<CommonEntity>(); Z5 @; a' M- k) n1 p
- public void init() throws ServletException {
* W6 C; ]9 A* l0 l - // 初始化阶段xml; N; K9 t/ P. d% n/ c% s
- String path = getServletContext().getRealPath("/file/stuName.xml");
2 A2 K) `+ u# U' K3 J# o2 x# L - try {5 f- ~& h' [1 P7 r: [
- listStu = PropertiesListUtil.listXml(path);) Y, T; ?$ V8 S0 ?' C
- System.out.println(listStu.size());# m( `1 b( Q' \* u
- } catch (Exception e) {
3 S: i# O( d. h' I a - e.printStackTrace(); I7 R* b" H! s
- }$ j8 n' i+ M# c
- }
- P0 K% J5 w$ }: [ - public void doGet(HttpServletRequest request, HttpServletResponse response)
' F3 u/ g$ B7 {8 t7 A; X# Z - throws ServletException, IOException {) T; Z/ j2 u# B, ?
- JSONArray array = new JSONArray();
. X8 X% U( e) L; u8 q$ o" | - response.setCharacterEncoding("UTF-8");* G @4 a0 R( V) L& k) \
- PrintWriter out = response.getWriter();# {1 ^4 I! K9 ~# o
- try {) u v7 P0 Q% V! J* |% z9 l
- for(CommonEntity entity:listStu){# S7 p& r, b! y$ a5 @9 ~7 M
- JSONObject obj = new JSONObject();3 z1 u6 c' u3 X7 I
- obj.put("code", entity.getEntityCode());
* {6 P4 X- H% h" m6 K6 Y9 ~ - obj.put("name", entity.getEntityName());
4 _6 I& r$ H0 |" _: v9 l0 K - array.add(obj);9 O8 f1 t' H6 T$ D: g/ p1 M/ i
- }
m! s) |) m( D8 j5 P2 I0 y/ G - out.print(array.toString());
: T& n) `+ I' G - }catch (Exception e) {
! R0 \8 b. m [7 Z+ k* k5 o2 k$ [ - e.printStackTrace();% u* M! o i5 k" b1 g% b: G
- } finally {
" t2 V6 E0 z) g- M0 G5 A - out.close();
H. L* [6 p- C% S3 I, C/ a - } X7 H2 B& h$ e7 w6 t8 u
- }/ f" U% o2 P: @) l" b: G
/ C- r5 U% k% p y, V3 J- public void doPost(HttpServletRequest request, HttpServletResponse response)
/ c# r3 y+ s( ^4 v. o5 K5 G, }% j: B - throws ServletException, IOException {
: e; D S+ [* Y( s% x0 n! n - doGet(request, response);# S6 Y8 U; R& {
- }0 t w, M6 Q- E5 n, s; E$ ^$ |
- 6 |! c; M6 k6 q, i: v" J8 h0 S" l
- }
1 c* g- D; ^# H/ E" w; s
复制代码
. |$ k! c( W. n. Qjava使用dom4j解析xml并展示demo(科帮网)下载:点击下载" }) r5 m1 [- Q% N2 b8 B7 v
4 t" A, f. M1 |. a) a) M7 C. o# w7 q
" `0 M+ l; M0 R J6 \
: \4 @/ P0 ~ \3 Z' O8 b! C: H Z( B
|
|