TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
java 使用dom4j解析xml并展示demo(科帮网):stuName.xml:
/ ]7 e$ m; [5 X/ q* q6 t ) c+ J! Z3 F3 j% ?5 G
<?xml version="1.0" encoding="UTF-8"?>
0 q& Y' J: }& D/ |- |7 x; W; z <result>/ R+ ]" Y: w6 w* Y
<question>
% r7 k6 ^+ R1 M8 o% r, m <code>001</code>
$ m( A* {" Z$ K' s" b/ M9 _- @3 L <name>张三</name>
% E8 I- q+ d) I2 [0 a7 c R3 z </question>
2 y& e* f. F/ t <question>
1 T* n8 m: W' Z' }" `8 e <code>002</code>/ k m6 j% y7 a' [4 k
<name>李四</name>5 A7 _9 `2 c+ }, B) q
</question>
+ f$ a% o- V5 q( e9 B <question>( z1 n9 I( l5 H; Z9 P+ E/ G: @+ {
<code>003</code>
" x) c1 V# @- W" G& r9 G <name>王二</name># h6 Q+ V/ R' Y1 f7 C' j5 F
</question>
" I* ]5 N* D' N' j" ~' Z <question>& U+ j; Y' R* Q& B
<code>004</code>
! g7 N' I, `( J; k <name>麻子</name>
$ P2 V% n0 c. N" Q; c </question>1 L; F- d) K# m T/ z# }. T1 d) K* c0 E
<question>3 }0 X8 n) C8 _0 P9 Y7 q
<code>005</code>. I: v& s' T$ q' g8 h. t6 l
<name>科帮网</name>! Z: D {: N' i2 @7 y
</question>
# I2 L/ `/ N( V: I: J9 i" u. ? </result> 复制代码 XmlReadUtil.java:读取方法
% ]+ S1 {6 j2 ^8 M package com.itstyle.util;1 u) N! s4 D S0 W
% J7 p' E# ]0 ~ C% |) D- I import java.io.File;0 Q+ q& A$ l& o# ?2 K/ F2 t, j+ C( D7 e
import java.util.Iterator;
0 G9 H5 a+ b! A5 t& G import java.util.Map;; S' H# N) `+ t& n8 Y' k
% p- z8 x% g2 \) o& [! c$ I
import org.dom4j.Attribute;
& ]' g4 F5 i: ~2 b, Y% b import org.dom4j.Document;( \0 W7 {" L2 R- l
import org.dom4j.Element;/ D2 s$ i' \! \& j- V
import org.dom4j.io.SAXReader;
9 {3 ?; J! d& q0 x# _ /**; P' D' T' ^5 F8 N0 u
*
4 [6 O" `& ]# R/ [- s" Z * @author 科帮网(www.52itstyle.com)
3 @" g4 _% {8 `' a *" R* C7 G* M0 ~; Z" m
*/
% ~) U7 h# ?& {2 e: F" _ public class XmlReadUtil {: J9 w- v0 j% M7 q: F: m
@SuppressWarnings("rawtypes")
1 r' j: }$ ]" Y/ B# s8 J6 t, l& U public static void listBaseInfo(String filePath, Map<String, String> map) {
! x) O% l5 F; o+ _0 d, K" y SAXReader saxReader = new SAXReader();
5 f& x4 n1 W+ t: X try {
) _, a) b" {/ f: }- u5 B$ Y) b Document document = saxReader.read(new File(filePath));7 b5 U; V! p' j# S% [
Element root = document.getRootElement();$ O5 W8 P3 J5 c6 w; h
// 用于记录信息编号的变量
% U6 M% u$ |3 K1 l int num = -1;
( n8 Z% g3 ]; n/ j* W" ?: y+ b* Y // 遍历根结点(result)的所有孩子节点(肯定是question节点)9 O: T/ s# `8 y2 X% q8 d
Attribute attrCode = root.attribute("code");
4 X2 A' i" C0 i/ s3 q& u Attribute attrName = root.attribute("name");: a' e! @! W+ D; _& ~2 v( ?
if(attrCode!=null){
' ^+ S' h. w7 g$ D map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
' k p, C$ u; O( v: t }
3 n7 V2 R0 r- I4 W if(attrName!=null){! S7 q; X; w1 n+ B! Y
map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());
0 G( j% \0 n& G2 @1 i' P }
- b, S: P% S0 f' ^4 t4 ~, a for (Iterator iter = root.elementIterator(); iter.hasNext();) {
5 _% F* v6 U& y- d- f5 q Element element = (Element) iter.next();
/ D4 P; x' y# ]. z8 e num++;( @2 P/ C5 z0 l
// 遍历question结点的所有孩子节点(即code, name),并进行处理
$ L4 ^+ O Z9 Y for (Iterator iterInner = element.elementIterator(); iterInner# k! i2 b: ?' N3 F
.hasNext();) {
( M0 [/ g3 y6 j2 C1 j- d+ O. C# T Element elementInner = (Element) iterInner.next();5 _$ m S7 f3 Z
map.put(elementInner.getName() + num,- b- ]' o" f4 V$ r p/ c8 q
elementInner.getText());2 Q/ A$ S0 x0 d/ I
}
) V6 P8 f* K) d2 n9 H# Z. _ }
8 c+ F5 [2 V0 S# g/ P; b+ Q - Y# E2 f7 a! s# e$ }
} catch (Exception e) {
0 ]5 a/ a i+ Z7 E5 w. a; T e.printStackTrace();
7 O. v. K8 v% Z1 M) p5 i }
) K6 U3 V6 q3 G" x# n ' ^& {- Z! {5 h
}, P& [0 C6 y/ Q# |9 H/ @2 C
}" v; O/ j: F% S' ]
复制代码 ReadXmlServlet.java:! u7 }/ g- u) s) T% j
package com.itstyle.servlet;
/ M6 R; g3 S/ m
- m7 `) R0 {7 w: u# l V7 d import java.io.IOException;
; v- N, p( l$ G( W9 y9 @8 \ import java.io.PrintWriter; Q$ O$ p, s& j; I: [
import java.util.ArrayList;( [3 C, p0 b% g. h/ j" V2 B% _) A
import java.util.List;3 h( ^5 K9 G6 k9 A+ @
1 H+ ]7 ?; G# q import javax.servlet.ServletException;
% r! m, Q/ R5 U/ C import javax.servlet.http.HttpServlet;# v' {2 I1 B* _9 A4 Q/ w" X
import javax.servlet.http.HttpServletRequest;
, R/ n7 U- i' c/ U. d. i import javax.servlet.http.HttpServletResponse;) T8 i3 O/ M, x5 [* P+ j
, m- L9 p, [5 d$ X import net.sf.json.JSONArray;6 N. o* f, f) K- X% O
import net.sf.json.JSONObject;
: F& B; X- i$ ^1 r8 A6 F
! P" K8 V8 e6 x* k. K# T) U3 ` import com.itstyle.model.CommonEntity;( k" J- @. j% ?6 _ u9 t
import com.itstyle.util.PropertiesListUtil;% W- p4 _5 F6 _1 T: R$ r+ P
/**% P3 p* p- y4 c- R
*
7 i; g% D3 t' R' c/ z8 r4 u * @author 科帮网(www.52itstyle.com)
9 G" a: @! n: b" t9 [ *
( m) F, A0 F6 S4 m }3 Y' A% [ */; u( J: g/ H6 B% z8 b( W
public class ReadXmlServlet extends HttpServlet {
+ b0 x, A; W) g% n ! j, c8 S4 Q5 r1 ] [' Y
private static final long serialVersionUID = 1L;
) e5 B# U: u, ]* m private List<CommonEntity> listStu = new ArrayList<CommonEntity>();# U: O# U9 v# e- w8 r# f8 L# z/ `
public void init() throws ServletException {
2 T- l+ i4 T% ] // 初始化阶段xml
9 o) V4 g! A! O0 n8 a: g i. G3 x String path = getServletContext().getRealPath("/file/stuName.xml");
1 a8 Q# r- s- a" H/ {* J. W try {: @4 L$ c$ t! w& D f$ j% K" r
listStu = PropertiesListUtil.listXml(path);! O J& O8 _6 u* `3 i% i
System.out.println(listStu.size());
# [/ ]1 s" h' U% V- T } catch (Exception e) {/ f1 q# G# J0 D1 N8 ?9 v
e.printStackTrace();! L) [1 L j, ]# s3 e; O4 V( u
}
: Z* g7 j2 I2 ^3 M* I' R# S }, h2 W2 T* |$ ^# K% L! m n
public void doGet(HttpServletRequest request, HttpServletResponse response)( G5 l! I3 j# L! L# \) S# g
throws ServletException, IOException {
8 @; y/ ], _5 q, O8 C4 m* T; m' H JSONArray array = new JSONArray();
, L: C& P1 i/ v) I$ ^" g response.setCharacterEncoding("UTF-8");0 w2 G- r; t" z# x, Q/ z
PrintWriter out = response.getWriter();
2 Y9 B* ~. o" O' |) Q' w$ [ try {# z! Z# w) L0 I+ y) m4 K
for(CommonEntity entity:listStu){9 e" D8 n; G! }, x
JSONObject obj = new JSONObject();
e3 r* O8 ^$ K! x obj.put("code", entity.getEntityCode());
s4 Y/ n3 b( O" J: p! U obj.put("name", entity.getEntityName());
) j+ l7 F3 ]4 y& { array.add(obj);# Q o" D* l2 {6 e7 F6 Y$ K4 _9 ]
}9 O2 T1 V# g+ E0 Z
out.print(array.toString());
3 }1 x; c& N3 V% _ }catch (Exception e) {
# ~ j0 \' K E) L e.printStackTrace();
9 n6 v% {. I' [ L7 N! i& b& ]1 a } finally {
0 P/ F% K, ?* \6 C1 I out.close();* z5 m7 O$ V& C# i, L
}
7 M: S6 A+ \% I$ S; U' W }
; F9 \; s4 Z- c4 ]7 `
. o/ n3 M4 X4 H2 j/ q public void doPost(HttpServletRequest request, HttpServletResponse response)6 f4 Y# j# y2 h1 W
throws ServletException, IOException {2 }% D! Z) W5 a3 K
doGet(request, response);
+ d# r6 R* u. Z }* D+ }5 @# G8 w$ V# g( d$ v7 {: d
3 J7 H9 l+ D8 }
}6 k, i# ]6 N7 Q" B7 Y$ Y
复制代码
6 b$ H2 @# q/ u% e5 a j java使用dom4j解析xml并展示demo(科帮网)下载:点击下载
, C. u: d* C8 c1 Q; a& I
: Q$ ~/ W9 i% {8 j+ S9 n) C I5 T; b) D }2 @
+ h6 i, b s5 V( G: F5 ]1 V
7 O. w. ^0 Y% q. u+ k
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :