TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:
1 w: _0 A5 \5 d' X+ i1 T5 |4 T
, y) D: b- X7 |- <?xml version="1.0" encoding="UTF-8"?>
1 |+ t: L4 ?& L; | - <result>: j2 x* D& J9 ^' P) B
- <question>" c+ E0 ~# W/ t- x) H
- <code>001</code>
3 R! |; |5 T! k q; D - <name>张三</name>6 b9 {8 C* i3 Y& ^
- </question>
) o& t& q! [3 ]/ F, n- e3 d8 R - <question>
6 h, i! A. I7 D: [ - <code>002</code>
f/ C3 h! a/ m+ t7 c - <name>李四</name>
( ~ y3 c" G# B9 K6 j) Y" T% K - </question>3 I+ r( v# k7 o
- <question> \+ D0 {" q& t
- <code>003</code>( @- |" j% W! L7 j; W2 r
- <name>王二</name>
8 `) d" }- R7 d- m; w- @ - </question>
2 Z6 x9 F6 U' l+ C" N, }( ]( { - <question>+ _1 ?8 k) y! n! X% c! M* X
- <code>004</code>) |0 E% s* ~ Y( U) G
- <name>麻子</name>
5 Q! H- ^6 c j, _% o7 X - </question>! p4 }7 l: I- c1 {7 ]
- <question>' [- |" v. ~( @5 l, U
- <code>005</code>" H$ D f) c' `/ Q
- <name>科帮网</name>( H6 N( ~5 H5 t. V# j' q
- </question>4 {. D! M. F3 ]7 R* i2 z
- </result>
复制代码 XmlReadUtil.java:读取方法
6 p# Y1 V, \# X- j" U+ M6 P- package com.itstyle.util;
7 T2 J9 m/ f1 ^: c- E - % p, A/ ]* k* d1 | X: h9 h$ \
- import java.io.File;3 _. M, i% i( N7 r
- import java.util.Iterator;
7 D1 {$ n- ` f6 \' V) B - import java.util.Map;
9 F1 q! n6 n1 T ^; h
$ ]) Y: L) u% I8 B8 P- x7 `* ^- import org.dom4j.Attribute;
# }7 I2 ?) j9 }) Y) }( M" e - import org.dom4j.Document;
1 d, V5 a b# R) z - import org.dom4j.Element;2 X4 b) T& o( `- Q
- import org.dom4j.io.SAXReader;! m/ }3 t2 e( b2 J
- /**
4 F2 N* e( n$ W/ w; U/ j k - * |% d' R8 j5 e0 E. q
- * @author 科帮网(www.52itstyle.com)
& q% Y5 P( `- ~2 ]! f. v \ - *! e2 _8 [- J v* B- L; E
- */
; t) N8 B0 X0 i7 i5 a6 J0 L9 K - public class XmlReadUtil {
4 ~; @5 y5 c. B4 z# P8 T- r+ f - @SuppressWarnings("rawtypes")4 r& O ~, A- C9 w: x% b0 ^
- public static void listBaseInfo(String filePath, Map<String, String> map) {
, q3 L' j8 W, T: g - SAXReader saxReader = new SAXReader();3 Y3 [- W- z: M- X
- try {
3 U# L+ F5 P# K - Document document = saxReader.read(new File(filePath));, n% h; @& `6 |3 z" C) [' I
- Element root = document.getRootElement();
8 l$ E" ~+ p+ R8 o5 s" n - // 用于记录信息编号的变量7 b2 w1 i1 }$ K
- int num = -1;" |) m8 f; ?+ ~
- // 遍历根结点(result)的所有孩子节点(肯定是question节点)6 f- r$ i0 `7 n0 R5 L& }- O2 o5 W( G
- Attribute attrCode = root.attribute("code");* Z7 `: Y( y0 o* @0 T6 t4 u$ {9 [
- Attribute attrName = root.attribute("name"); Z: l) B8 ?/ L
- if(attrCode!=null){
9 i# A) u7 B3 s+ `% _$ u" Z - map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
: c0 _0 t* q) Z% Q. E+ I# j - }) |9 M# v: I! h
- if(attrName!=null){
" x9 ?- y; D- C - map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());
0 D7 s" ~" _. h! l1 R2 ?6 z' e% g - }: z5 _2 c5 }7 c+ d
- for (Iterator iter = root.elementIterator(); iter.hasNext();) {
* j- w6 P- k* Q0 O0 v - Element element = (Element) iter.next();
9 U$ e- w3 ~5 W - num++;
3 s7 e! F* E1 X" L/ O - // 遍历question结点的所有孩子节点(即code, name),并进行处理
2 [ P( W; Q2 B! r1 | - for (Iterator iterInner = element.elementIterator(); iterInner
) j. O) |: J: }% c" D S - .hasNext();) {' D6 D) `' ~8 E& f0 ]- ]
- Element elementInner = (Element) iterInner.next();
% l0 F% w7 c8 {4 ]3 `+ G - map.put(elementInner.getName() + num,
8 I: l5 \# P+ t* @2 t; |2 ` - elementInner.getText());
/ v/ t! X# ]$ H, F! L - }
4 F+ j5 f& f4 a4 K& Z' V7 i - }* J" @1 I0 _0 _) ?' T% k8 Y
: {2 I/ a5 [! w( H# c4 [. Y- } catch (Exception e) {" L3 ?8 |( f( n- n+ N/ x2 Q; V
- e.printStackTrace();
2 y& M% ~# s1 |9 ?1 w - }! P$ V6 b2 @6 t# c! ?" Q
1 t) n6 S: \! Q- }$ D1 H$ }) t: L ]/ L: u
- }* p. V1 y) B# N0 k. [. N. O
复制代码 ReadXmlServlet.java:
! E$ o2 G; C c3 _- |4 }- package com.itstyle.servlet;
: f4 z6 ^/ G% U8 S6 F( T& H - 4 [ S' _1 s- a4 e0 n
- import java.io.IOException;5 Q, D2 M- S. f
- import java.io.PrintWriter;
; e! D* h, Q$ T - import java.util.ArrayList;' [/ x- `% a6 P9 G) g
- import java.util.List;& X k3 X: g4 s
- . k6 n: o+ m, M) x! q) u1 s; ^
- import javax.servlet.ServletException;
% h' q5 \$ t4 g) | - import javax.servlet.http.HttpServlet;. b& S- k, ^/ T x
- import javax.servlet.http.HttpServletRequest;( _/ l# y0 I6 W, n V6 [
- import javax.servlet.http.HttpServletResponse;
T* j( M% ~' ]7 X* s. u0 ~ - 4 k2 z2 X- N) z1 j- B
- import net.sf.json.JSONArray;# X- j) E/ F/ i. t/ y
- import net.sf.json.JSONObject;/ k$ ?& D6 z8 d/ ]
* Y3 x7 y4 w2 J/ E% n* @- import com.itstyle.model.CommonEntity;
! k j5 Q8 V% L3 Z& f) s3 v - import com.itstyle.util.PropertiesListUtil;1 A4 G1 X! p8 I. `9 m; y8 Y% {9 C0 n
- /**# } Q- f; _' c4 _' t/ N6 {" S
- * 2 L9 _( y8 b+ ]) ~
- * @author 科帮网(www.52itstyle.com) ^4 X( M* x. J! O
- * }7 O% D B0 b7 J# _) R
- */
`& J+ X% z1 Q* \9 {) g; c( g" }( c( } - public class ReadXmlServlet extends HttpServlet {& G7 p6 V& m! { U* e! D
- 6 P& N2 R. ^) n9 b$ L6 d
- private static final long serialVersionUID = 1L;* n7 S% y" {* O' V" G1 Y! l
- private List<CommonEntity> listStu = new ArrayList<CommonEntity>();& q w/ X8 M$ \0 j5 I
- public void init() throws ServletException {
2 D! r" ?7 `3 x; n - // 初始化阶段xml$ G$ W0 M5 o5 e
- String path = getServletContext().getRealPath("/file/stuName.xml");% z8 v: `9 ^% U# H! E# G- t& w
- try {0 }& g& A( c7 I* T
- listStu = PropertiesListUtil.listXml(path);* k% u* z6 Y+ X n7 R) o3 J
- System.out.println(listStu.size());
1 \5 x8 U. N7 d: d" Z8 O5 _6 l - } catch (Exception e) {
" l+ n: Z6 K* }/ y- ] - e.printStackTrace();# n( |% S* @, k n4 L
- }: F" O; |7 F9 j% @
- }
) q% B0 t! m! ~3 L - public void doGet(HttpServletRequest request, HttpServletResponse response)) D6 P G8 ~4 R! H, j) K$ s
- throws ServletException, IOException {
& w) C/ d( r$ _, u+ _9 D2 h) N! M' o R - JSONArray array = new JSONArray();
1 `5 w2 V1 q& X( b' e - response.setCharacterEncoding("UTF-8");
5 A+ y( B- z; K: B Y, g1 `$ s7 r0 M - PrintWriter out = response.getWriter();
5 r( z8 @2 U' W8 l& {$ I7 ~ - try {# P% K6 R1 V7 _7 f4 H) n, T& ~
- for(CommonEntity entity:listStu){9 u- n+ ? e6 x
- JSONObject obj = new JSONObject();0 a d) b* s: O7 r% I
- obj.put("code", entity.getEntityCode());
& q" d6 T ]# | J( V$ M - obj.put("name", entity.getEntityName());+ c; _! j: q8 l5 D: T3 |
- array.add(obj);- g; |$ q7 u/ M2 k6 K) j0 T4 G
- }) ~; ^: [ ^% q
- out.print(array.toString());4 R' {/ f' F2 H3 X
- }catch (Exception e) {
& |1 ^, K7 B6 W& K5 R3 n - e.printStackTrace();
6 v! }2 u4 E2 h8 ^$ `0 d4 s - } finally {
9 @# e# h$ d \% L9 g" D/ ~ - out.close();
. h4 Q5 j( D/ R. Q$ i$ b( J - }
; v/ h. V) F. H, a* t' q, h+ L - }
6 O/ e; P3 Y! b6 Q* U; Z0 n. s - # }: Q) Q& i- g6 p
- public void doPost(HttpServletRequest request, HttpServletResponse response)4 f5 P$ P$ d& n* A/ \
- throws ServletException, IOException {
) k$ u" e* m4 x4 v4 {8 m" h+ ] - doGet(request, response); z0 j; Q8 N( S0 t/ z/ [
- }) c+ H% G J, E- n
' E1 S# E5 e2 e% H4 I- }
4 d$ y' @: X& H4 I6 s8 I
复制代码 # P: b. S) `$ B2 B' G
java使用dom4j解析xml并展示demo(科帮网)下载:点击下载
5 i/ d( M1 d, I
% I/ B6 o: `% k$ L' B: C* ?/ G& t) Y
* C, B- a4 r) w) e" w7 ^
7 T' @ d/ y* p0 ~. t8 ]1 F1 w |
|