TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:6 d+ q0 b: B! S
0 T+ W- X% z% V* U I
- <?xml version="1.0" encoding="UTF-8"?>: P& p: P& p/ H9 K7 j
- <result>. k& R" \6 q% O7 r- M, E
- <question>
2 \" B+ g) o7 u - <code>001</code>
0 Y) V7 D9 J- ^6 R- P - <name>张三</name>
) B1 N( J1 `6 Y: |- t ] - </question>
. K9 e2 u( D" W6 @/ G - <question>
5 Q3 Q4 R; k1 f, |9 Q4 H - <code>002</code>" M+ g; ~0 P9 h z# L
- <name>李四</name>3 c4 ?: z' m4 O! G* f# \
- </question>% V) I7 [& I5 ?+ H
- <question>8 |- G0 l# Z3 J! s2 }* y% ~
- <code>003</code>1 }: `6 @3 C- H6 W9 v, _
- <name>王二</name>
( e* `( s/ A0 r9 _( X - </question>
5 U& Y% a x ^6 V1 m5 |# D: _8 b - <question>) T6 S) ~: F H: o+ ` P
- <code>004</code>
. M7 ]+ ]$ y& R" @% x9 }4 q - <name>麻子</name>, N' F; R* d% m2 g
- </question>
# d; z8 I) x. D! {- b6 r. G' ] - <question>
+ I" i: H6 [' i: P2 f - <code>005</code>
& e" H& i* G, j% r" g3 t - <name>科帮网</name>
1 t3 _' W) ?. O, k6 h - </question>
1 o% o) N* q# F5 ?$ q9 q - </result>
复制代码 XmlReadUtil.java:读取方法
! M) |/ Y0 P. N! w* `- w' R- package com.itstyle.util;/ K, k. k7 R z3 [1 z# ^9 \. J1 K
- 7 j) g ~ }0 a- B7 F
- import java.io.File;
6 p3 R: [& ]. v5 `3 s( f6 d; U - import java.util.Iterator;' R' A# `2 ?$ V4 {& j% d9 o
- import java.util.Map;
- A( L: t/ ` ^ - . O* V& M; S& D! J9 W2 W$ T
- import org.dom4j.Attribute;
& D& Q% ?1 M; w6 L, [$ \6 c2 s - import org.dom4j.Document;
. x8 l% b3 A/ C: V8 D" ^1 l - import org.dom4j.Element;
, ^1 A- [8 C( Y9 a& U - import org.dom4j.io.SAXReader;
4 ^# n! K1 b8 f3 m! [+ |2 w - /**
4 Z# z. I$ b9 q4 V8 ? |5 R - *
0 \8 m6 K7 y4 {1 O8 V& } - * @author 科帮网(www.52itstyle.com)3 p4 \4 O: K* l* T* `; x
- *5 q8 ]! b Q/ V+ }3 k
- */9 E1 B( }5 E- U5 X8 p5 s# x* \
- public class XmlReadUtil {. K y6 w ?# |! f4 j, ]% y
- @SuppressWarnings("rawtypes")0 a4 f; }* u0 n, C5 l
- public static void listBaseInfo(String filePath, Map<String, String> map) {9 ?* N; N7 p% @: ~$ z: e4 q+ n
- SAXReader saxReader = new SAXReader();+ s) Z7 p4 _4 r% a+ ^2 K( B
- try {
5 u# d8 W& n4 o9 _( D- P# N! K - Document document = saxReader.read(new File(filePath));
* x% R" _6 e7 r* n7 H - Element root = document.getRootElement();' ?, b7 ]. x% j" u( |! n) u, Q
- // 用于记录信息编号的变量
7 ~/ s8 g+ M/ V( Y - int num = -1;
0 x' l- L7 E2 n- Q! B0 R3 B/ b - // 遍历根结点(result)的所有孩子节点(肯定是question节点)- ?- I# }% o' E. n+ y3 I) w4 s: J
- Attribute attrCode = root.attribute("code");! R; r( Z0 j) Q0 a G; D7 e1 x
- Attribute attrName = root.attribute("name");; o8 K" p" P) w8 }7 \4 q2 X P
- if(attrCode!=null){
2 l+ |8 S2 B) {+ d! g% k; V - map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
, Z4 U; G' e# Y - }
* S" g6 l1 c- X6 [ {% y - if(attrName!=null){4 m. Y3 Q( s& i, j0 |
- map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());
/ e- w; z8 q P1 Y - }
. ]: G% }4 z9 B: O1 d7 u( V! [' O - for (Iterator iter = root.elementIterator(); iter.hasNext();) {/ N3 Q7 O K0 |) e% W# `6 Y8 e; R
- Element element = (Element) iter.next();- X" k1 V' l+ ]
- num++;0 H( k- U0 ]& O; r: P, P: t, r! U9 X
- // 遍历question结点的所有孩子节点(即code, name),并进行处理
9 S, ]; |9 Y+ W) q6 Q' o - for (Iterator iterInner = element.elementIterator(); iterInner
! T" `1 M6 x' b- p7 f/ H0 p8 q$ W# K5 I - .hasNext();) {1 {6 M. `4 {3 C ~3 f
- Element elementInner = (Element) iterInner.next();$ s- ~+ D1 U8 q, W1 j3 e
- map.put(elementInner.getName() + num,/ @& g, M0 z7 N( u* Z
- elementInner.getText());
* v0 D( R7 U& m9 |8 a - }
1 I5 V* L( L' ~0 C5 n; s/ u7 \ - }
% X" C. h1 p0 r3 f7 j1 |$ s
* U) G- s7 @) b- O- } catch (Exception e) {
6 S: L, H/ u) ] - e.printStackTrace();
* S) M* X" F3 w0 [0 `1 b/ V - }$ ~0 k- O) A1 a% n
/ v# n" a8 m) l1 h$ {" S A. t- }8 r" N5 m3 n8 T" k, F0 P, w% f) W
- }- l+ Y3 ^3 E d9 S |( j' }
复制代码 ReadXmlServlet.java:5 t5 y/ X8 d+ r# }8 Z
- package com.itstyle.servlet;) l1 q$ v i& d( c
- 7 y2 [$ p. X1 Z( b
- import java.io.IOException;
; m7 |& g. {& R/ F4 d& ~ - import java.io.PrintWriter;' U9 @1 |0 _: M: g5 L
- import java.util.ArrayList;8 S0 I U) z( r a9 y) C
- import java.util.List;
! m$ P6 g) @$ Z1 l5 x1 y
/ T" h8 U# d Z% X# p" l; Q- import javax.servlet.ServletException;
# W3 q4 C* c' [- i$ E" {7 Q1 b - import javax.servlet.http.HttpServlet;7 v* Z* C+ [4 ~, P* I% o
- import javax.servlet.http.HttpServletRequest;1 I* f c& a- w- M
- import javax.servlet.http.HttpServletResponse;
% j2 m- J; w# l5 _. e3 x Q
0 }' v F! b8 H/ Y( X$ g- import net.sf.json.JSONArray;/ I) {4 h, h* }0 _+ {5 j
- import net.sf.json.JSONObject;# j O" w3 a( g, K: v$ }
$ |2 g( p' C$ g- import com.itstyle.model.CommonEntity;& g7 m }# F8 J' q
- import com.itstyle.util.PropertiesListUtil;
. a8 j% M+ x* w% B3 o( I - /**
& E: z! j) i8 ^/ b2 H - *
5 o- _& c& B9 _5 X" N - * @author 科帮网(www.52itstyle.com)
r( ^, m2 P; ]7 F' H - *4 q. W9 k6 l; S6 v
- */1 t5 |) @0 f8 x- P
- public class ReadXmlServlet extends HttpServlet {2 o" Y; N- Y+ X- v7 ]
# U6 i+ r S" f' D3 _- private static final long serialVersionUID = 1L;7 n/ C3 m$ { R; t# W, N5 p/ D
- private List<CommonEntity> listStu = new ArrayList<CommonEntity>();
' J) {9 v9 H }& [0 J - public void init() throws ServletException {1 s2 _3 _" Y# w( G/ K+ z, b
- // 初始化阶段xml
: P! h! F- W5 \9 Q9 X- P$ M - String path = getServletContext().getRealPath("/file/stuName.xml");1 \& e7 Z, Y! j6 s% P9 V% f
- try {
" l: w; Y* B% f0 h) T - listStu = PropertiesListUtil.listXml(path);
4 x1 Y7 {# u. \6 P$ b/ L% M - System.out.println(listStu.size());5 H j# k( G& }( u) }/ E9 C
- } catch (Exception e) {, Y& V0 |+ f9 s0 @
- e.printStackTrace();
. f. h* M- K% w0 @ - }) I$ Z- K9 w; \ B5 N# O
- }
' R5 M; H* ]( d& s6 e3 b - public void doGet(HttpServletRequest request, HttpServletResponse response)) c9 v4 l# x- c3 x: {$ N I. _# H
- throws ServletException, IOException {
V5 }# u; m$ o8 g - JSONArray array = new JSONArray();; z1 M* U& i6 M2 k7 D1 P
- response.setCharacterEncoding("UTF-8");/ S: E: H; W0 ~) b* D1 m9 P
- PrintWriter out = response.getWriter();1 o/ h3 v% w z' v
- try {: z! T9 g3 }6 {2 B
- for(CommonEntity entity:listStu){
" G. T# X8 U1 L& c2 q3 l - JSONObject obj = new JSONObject();& o& W) |$ g% _; }# _' P) k: V4 Q
- obj.put("code", entity.getEntityCode());
3 I; `1 y9 e% P2 C - obj.put("name", entity.getEntityName());
5 [1 x+ m F& W( O6 k6 j - array.add(obj);- n% y% a$ K3 j
- }
+ Z8 K9 q8 E8 Y6 _ t - out.print(array.toString());2 I4 T ]8 I, z$ J3 W
- }catch (Exception e) {: P+ M, Q' R1 w" w8 N3 i
- e.printStackTrace();4 P9 Q6 h0 i% f& U1 }
- } finally {: c7 N9 v$ m6 [# i- @, M" g
- out.close();
0 x# p$ T/ `5 V0 j3 ^* `# P4 Z - }
6 Y$ Y% [) W% P1 a3 t - }) s8 x6 ^1 v2 ^/ {, P7 k; E2 j
- 5 ^0 F3 {! | r% }* `- d
- public void doPost(HttpServletRequest request, HttpServletResponse response)0 Q. S! J L6 \% f S, a$ N
- throws ServletException, IOException {! P3 t X1 y- b' B1 C' B+ d. C
- doGet(request, response);
% p2 I7 H1 r" s" j3 Y1 T6 s - }4 }" Q$ y/ a2 v0 V3 R$ u& y
$ M5 Y0 `+ g7 D5 @/ t5 u- }
2 i) P& t- I e+ E) q1 u: B0 X
复制代码 - Y g; o; q! ~9 z: Z! Q
java使用dom4j解析xml并展示demo(科帮网)下载:点击下载$ I: H/ _: Q) D( n- |, y
( P/ J! s1 \3 ^" ^
7 ]- n- C8 L0 E2 D) L; T" D
2 z- I/ ~) Y; Z1 }4 t4 r8 t
0 h; m+ J2 D" F7 u0 k" }' |! V |
|