TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:
3 S& h L) z, L% @( O8 H
0 D8 N0 ~- v$ ?/ ?: i9 |- <?xml version="1.0" encoding="UTF-8"?>/ [- z! J0 a4 u K* g
- <result>
5 Z3 ~. @* X3 l. i% K5 P1 c# V - <question>2 R; A+ z1 m* E& H; X7 }9 L1 D/ w6 u
- <code>001</code>
, Z- }8 ]$ E) t- r9 h" [, P+ d - <name>张三</name>- S& \, e# V. `9 ]1 D$ P# Z! `- N
- </question>; ^2 _* I1 ~, a) v: E/ B! ?9 {
- <question>
2 o. z" j8 t3 _: O9 F - <code>002</code>
3 y# I& V8 A1 O - <name>李四</name>7 I- n% T @' ]% F. g! C5 O
- </question>- w4 l' @' X# m; Q, Z S( I
- <question>. }8 P% b. l9 I/ I
- <code>003</code>
# Z) ~4 }0 J- @ - <name>王二</name>
) y U: M2 V6 w6 p6 U - </question>
8 `5 n2 t: a* _ - <question> p" @" q' [( Q0 x l- `! B# G4 k
- <code>004</code>
* y5 t4 Q# e/ k7 G& {8 H f. F' `! P - <name>麻子</name>
, k5 ~- L* R9 u6 f: z; A - </question>
& E% n% t \2 [! V+ ~) Q6 Q6 x2 A8 N - <question>
8 B; t9 e. x3 u0 P" r# Y) j$ B& T - <code>005</code>0 e- _5 e3 D* w" }" q% L$ r
- <name>科帮网</name>+ h* k, C9 ]+ ]8 I6 r
- </question>
' T4 m% s; G1 s; l) T- M" k - </result>
复制代码 XmlReadUtil.java:读取方法
. k& V" Z& b0 X% n' g- A( |$ |- package com.itstyle.util;
4 S# _ c& z, z- [4 d
# ~8 L( a3 M B3 J- import java.io.File;1 c7 ]$ o* e* ]( L
- import java.util.Iterator;* @: U* C5 k$ D: B5 s9 J7 p$ f
- import java.util.Map;7 @4 @! E! M/ U- B. S# V
- ( @* G. C! L2 T$ O
- import org.dom4j.Attribute;: ~2 b# j7 I$ T6 k7 V
- import org.dom4j.Document;5 c/ T2 ?5 u- ^' n& A
- import org.dom4j.Element;
1 ~$ d! R8 r! V5 e) t' ^ - import org.dom4j.io.SAXReader;
8 W( P8 k2 w1 A3 R/ d4 ?" E - /**
" p6 P8 H3 Y$ U! V2 X - *
2 T* l# t v4 `$ q, X; s - * @author 科帮网(www.52itstyle.com)9 g7 Z8 h4 C7 W$ P# D" z
- *( |' p& _4 V' h% E8 x, }
- */6 V* ?- Y9 H( ?0 j/ E% j
- public class XmlReadUtil {2 v, T* f2 w% D/ Y2 ^# P. d9 ~
- @SuppressWarnings("rawtypes")
$ l0 ^ E; B$ R4 y$ m% o - public static void listBaseInfo(String filePath, Map<String, String> map) {1 }0 c" m0 l7 [* |3 t& X0 S+ o, {
- SAXReader saxReader = new SAXReader();) W ]6 h4 {, d4 P. _. B3 s
- try {/ ~0 u' M5 n" t9 T" w8 h2 [
- Document document = saxReader.read(new File(filePath));" d5 x. d& W+ K4 ?/ h
- Element root = document.getRootElement();
! [+ i, T! Y$ W# B( M/ N# N9 | - // 用于记录信息编号的变量4 y! [* p, U' ?( |2 R/ k5 S
- int num = -1;
' `2 w$ e2 |6 ?: |% V3 } - // 遍历根结点(result)的所有孩子节点(肯定是question节点)
C6 Z% T8 S0 A$ I$ ] u# I# s/ K - Attribute attrCode = root.attribute("code");
w( H' a% d* w* G7 m+ ~ - Attribute attrName = root.attribute("name");5 a: G8 ]1 b/ ~8 W- A( J0 W
- if(attrCode!=null){
- `6 u6 u; I K: a/ N: C - map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());5 Y+ O9 A9 \2 H; r% m
- }0 T3 K1 _) ^5 S. ?
- if(attrName!=null){/ D( ^! q$ ~1 ~4 h- \% @
- map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());; [. _+ \- @9 b* Z' H, p
- }
5 `6 ?( r; N+ F* }: r - for (Iterator iter = root.elementIterator(); iter.hasNext();) {
7 p3 k4 {8 i+ Z- b - Element element = (Element) iter.next();
' A8 A' P& |, _' p) g - num++;9 j. Q: d8 K. U; [! |; W
- // 遍历question结点的所有孩子节点(即code, name),并进行处理* u8 u3 p% B7 q9 V6 r h* T9 I: [
- for (Iterator iterInner = element.elementIterator(); iterInner
. {: U9 c2 x$ B$ b0 |0 p O& T - .hasNext();) {
9 ]" Y; e. V$ l* ^8 |7 x2 Z - Element elementInner = (Element) iterInner.next();6 d/ _5 \# V- b B+ d0 V
- map.put(elementInner.getName() + num,
* `: L. ]. P. Z, X m; e% T+ b - elementInner.getText());
9 _' v+ X7 X3 N' m8 }" n# k - }
/ T6 a3 Z+ b, d H7 u - }6 C7 ~6 z0 ]4 v
- % H0 V$ J( @) [. X2 h/ x
- } catch (Exception e) {
- `' m+ M5 g' j' g# S - e.printStackTrace(); P* O2 Y0 |! I
- }( Q x c: {4 G9 a K
- / R1 E& ?4 F: p; {% R% D
- }8 k E! K% I' y# Q6 k+ r& C
- }
& J1 _% T' D) x- ?3 r
复制代码 ReadXmlServlet.java:, |: P; ]2 c1 U" F
- package com.itstyle.servlet;
) o' ~6 A9 w G2 z# j - ; }" f4 T8 Q2 Y; d" P- q0 o
- import java.io.IOException;0 F4 }, ?8 s* j% a. f' o; T3 r
- import java.io.PrintWriter;. c- x$ R+ X2 a% T
- import java.util.ArrayList;/ w9 g. u! Z9 Z; z
- import java.util.List;- d( B7 m8 m# ~7 U r
- # u% ]5 e6 w; U1 P9 M; L$ b
- import javax.servlet.ServletException;
$ O" w7 _; a" E5 O - import javax.servlet.http.HttpServlet;
/ i' H, K% T$ x* |% K - import javax.servlet.http.HttpServletRequest;! S1 e- a+ }6 C* \. J+ `2 B
- import javax.servlet.http.HttpServletResponse;% B5 f, K$ @ c. s
8 e) J( Y! R, |$ R- {+ V- import net.sf.json.JSONArray;
H. P. E8 m" t( X' \; t - import net.sf.json.JSONObject;( d) D# {% F6 j1 w
9 R T b1 }; Z/ z6 j- import com.itstyle.model.CommonEntity;
' B4 [$ \5 }9 B+ E! A - import com.itstyle.util.PropertiesListUtil;1 M4 I) T: z ^; r* k
- /**, k. N U7 K6 w# u! D9 d8 F: F
- *
9 s7 E! |! p5 B( ^ - * @author 科帮网(www.52itstyle.com)
$ ~( M1 c' N6 s - *
+ i7 K4 q1 M' I - */
2 y; ?+ X- [9 v# G - public class ReadXmlServlet extends HttpServlet {% m) m0 z4 j1 c& |
! T) i1 ?9 }% W# m- private static final long serialVersionUID = 1L;
4 w- l# B# _! c9 C9 ^ - private List<CommonEntity> listStu = new ArrayList<CommonEntity>();
8 m2 C7 w3 J4 ^( J - public void init() throws ServletException {2 o" T8 i, c7 k: D3 g" R( U
- // 初始化阶段xml
& U3 o) `4 C1 I/ M p- H - String path = getServletContext().getRealPath("/file/stuName.xml");
8 W6 f R% S. D+ ~; m4 d- i7 I* t - try {$ R2 s& c7 L: E$ X* h6 m9 ~/ o0 U
- listStu = PropertiesListUtil.listXml(path);4 L: K7 ?' X' {1 R
- System.out.println(listStu.size());' C1 ?5 \: ?. s _1 {
- } catch (Exception e) {4 W" X- c L! p& v% J J7 `) ~+ \# I
- e.printStackTrace();
# ^- Q' g2 B+ r - }
+ @4 |5 `: ~# P1 b) P. n' {8 c - }
U+ E2 w$ i- ?7 h- U. b0 v - public void doGet(HttpServletRequest request, HttpServletResponse response)
9 d/ o, Q$ Y/ a) v' h' w - throws ServletException, IOException {
4 W( d( I. z' [& @ - JSONArray array = new JSONArray();
b+ p" q+ B' L. Z& |2 Q - response.setCharacterEncoding("UTF-8");" |. `0 ]; O/ Q- x
- PrintWriter out = response.getWriter();
1 N0 z* m/ B6 {+ V; ] V# c- o! P7 R - try {
4 _, g% F2 O. U8 G7 f+ F - for(CommonEntity entity:listStu){
0 J3 H- ]7 K' \" j; y; t - JSONObject obj = new JSONObject();
0 N2 r0 _( R0 I% [3 t; J5 @( l - obj.put("code", entity.getEntityCode());9 \+ ~ x& x) A0 v4 {4 L
- obj.put("name", entity.getEntityName());
; V2 i9 J) C0 L, E7 `5 H - array.add(obj);
; n) m* L( S( m/ p* G/ F$ E - } c6 h) e3 d( @( u0 G+ p* G
- out.print(array.toString());/ y0 }$ I3 D2 n9 o& L9 n" S
- }catch (Exception e) {' k6 @$ a: m& v( R1 N. q3 ^3 J# @+ C
- e.printStackTrace();) o8 x* V/ P# t% v1 P
- } finally {) |# v" q2 w# F; G& w
- out.close();
5 g' V( Y+ G3 A+ e, i - }, ]$ v4 H, T9 D* Q. d
- }# |7 F* f5 E* h! T! l
- ) l! D" l4 `! ]: d; h# D
- public void doPost(HttpServletRequest request, HttpServletResponse response)
6 C3 }6 I3 [5 S; K, Y* C# a - throws ServletException, IOException {; G7 ]( R8 [5 S8 }0 j4 o& T/ k
- doGet(request, response);) l5 r/ |; g/ t$ p3 V4 _
- }' i" B" \4 g- Q% S* F- H
- 2 u& \) {& D S$ Q# K7 R# _+ `
- }% V& |' |( F1 a' L9 |2 j# F
复制代码 * a; ]8 W7 S$ E( n6 N1 @
java使用dom4j解析xml并展示demo(科帮网)下载:点击下载4 `+ _# ?2 U* e* h! T. J$ B- U$ _, Y
6 ?# n$ O- e. C" Z4 Q
* z6 }+ f+ P! C
" P8 J( j# j) w( J4 U( k7 w9 P4 t5 r( B$ l& W) T7 W% E: T0 Z1 `
|
|