dom4j解析xml文件,将内容显示在jsp如何做?
<?xmlversion="1.0"encoding="UTF-8"?><Stata><Bdataprocess="10%"rsenumber="1590"ptime="...
<?xml version="1.0" encoding="UTF-8"?>
<Stata>
<Bdata process="10%" rsenumber="1590" ptime="29/7/2008 09:45">
<rs rsname="B5ZB82_1" rsstate="no" rsinfo="标V-北-B82">
<rse rsename="B5ZB82_1" rsestate="no" rseinfo="标V-B82-右幅-桩"/>
<rse rsename="B5DB82_1" rsestate="no" rseinfo="标V-B82-右幅-墩柱"/>
<rse rsename="B5HB82_1" rsestate="no" rseinfo="标V-B82-右幅-左幅-连接部"/>
<rse rsename="A5ZB82_1" rsestate="no" rseinfo="标V-B82-左幅-桩"/>
<rse rsename="A5DB82_1" rsestate="no" rseinfo="标V-B82-左幅-墩柱"/>
</rs>
<rs rsname="B5ZB81_1" rsstate="no" rsinfo="标V-北-B81">
<rse rsename="B5ZB81_1" rsestate="no" rseinfo="标V-B81-右幅-桩"/>
<rse rsename="B5DB81_1" rsestate="no" rseinfo="标V-B81-右幅-墩柱"/>
<rse rsename="B5HB81_1" rsestate="no" rseinfo="标V-B81-右幅-左幅-连接部"/>
<rse rsename="A5ZB81_1" rsestate="no" rseinfo="标V-B81-左幅-桩"/>
<rse rsename="A5DB81_1" rsestate="no" rseinfo="标V-B81-左幅-墩柱"/>
</rs>
</Stata>
</Bdata>
一上是XML文件。
用dom4j如何将查到的xml文件显示在jsp页面上。
请将类和jsp页面代码写详细些。。谢谢啦。。 展开
<Stata>
<Bdata process="10%" rsenumber="1590" ptime="29/7/2008 09:45">
<rs rsname="B5ZB82_1" rsstate="no" rsinfo="标V-北-B82">
<rse rsename="B5ZB82_1" rsestate="no" rseinfo="标V-B82-右幅-桩"/>
<rse rsename="B5DB82_1" rsestate="no" rseinfo="标V-B82-右幅-墩柱"/>
<rse rsename="B5HB82_1" rsestate="no" rseinfo="标V-B82-右幅-左幅-连接部"/>
<rse rsename="A5ZB82_1" rsestate="no" rseinfo="标V-B82-左幅-桩"/>
<rse rsename="A5DB82_1" rsestate="no" rseinfo="标V-B82-左幅-墩柱"/>
</rs>
<rs rsname="B5ZB81_1" rsstate="no" rsinfo="标V-北-B81">
<rse rsename="B5ZB81_1" rsestate="no" rseinfo="标V-B81-右幅-桩"/>
<rse rsename="B5DB81_1" rsestate="no" rseinfo="标V-B81-右幅-墩柱"/>
<rse rsename="B5HB81_1" rsestate="no" rseinfo="标V-B81-右幅-左幅-连接部"/>
<rse rsename="A5ZB81_1" rsestate="no" rseinfo="标V-B81-左幅-桩"/>
<rse rsename="A5DB81_1" rsestate="no" rseinfo="标V-B81-左幅-墩柱"/>
</rs>
</Stata>
</Bdata>
一上是XML文件。
用dom4j如何将查到的xml文件显示在jsp页面上。
请将类和jsp页面代码写详细些。。谢谢啦。。 展开
4个回答
2015-12-14 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
jsp中解析dom不推荐,建议在后台用DOM取好后返回给前台jsp页面显示:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Baidu {
public static void main(String[] args) throws Exception {
List<String> list = display();
Map<String, String> map = run(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i) + "--" +map.get(list.get(i)));
}
}
private static String path;
static {
path = Baidu.class.getResource("/test.xml").getPath();
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
// 得到normativeField中每项的值
public static List<String> display() throws Exception{
Document doc = getDocument(path);
NodeList list = doc.getElementsByTagName("normativeField");
List<String> normativeFieldList = new ArrayList<String>();
// 得到normativeField中每项的值加到normativeFieldList集合里
for (int i = 0; i < list.getLength(); i++) {
normativeFieldList.add(list.item(i).getTextContent());
}
return normativeFieldList;
}
public static Map<String, String> run(List<String> list) throws Exception {
Map<String, String> map = new HashMap<String, String>();
Document doc = getDocument(path);
for (int i = 0; i < list.size(); i++) {
NodeList nodeListlist = doc.getElementsByTagName(list.get(i));
Element e = (Element) nodeListlist.item(0);
String value = e.getAttribute("value");
map.put(list.get(i), value);
}
return map;
}
public static Document getDocument(String path) throws Exception {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(path);
return doc;
}
}
控制台:
ID--eye1.id
xingming--eye1.name
xingbie--eye1.sex
nianling--eye1.age
shili--eye2.eyelight
shengao--eye2.high
tizhong--eye2.wealth
guomin--
gongfeizifei--eye1.pay
lianxifangshi--eye1.telephonenumber
bingli--eye1.content1
test.xml要在工程根目录下:
<?xml version="1.0" encoding="UTF-8"?>
<dataServiceResource>
<databasetype>relation</databasetype>
<normativeResource>
<normativeField>ID</normativeField>
<normativeField>xingming</normativeField>
<normativeField>xingbie</normativeField>
<normativeField>nianling</normativeField>
<normativeField>shili</normativeField>
<normativeField>shengao</normativeField>
<normativeField>tizhong</normativeField>
<normativeField>guomin</normativeField>
<normativeField>gongfeizifei</normativeField>
<normativeField>lianxifangshi</normativeField>
<normativeField>bingli</normativeField>
</normativeResource>
<sourceFieldsMap>
<ID value="eye1.id" />
<xingming value="eye1.name" />
<xingbie value="eye1.sex" />
<nianling value="eye1.age" />
<shili value="eye2.eyelight" />
<shengao value="eye2.high" />
<tizhong value="eye2.wealth" />
<guomin value="" />
<gongfeizifei value="eye1.pay" />
<lianxifangshi value="eye1.telephonenumber" />
<bingli value="eye1.content1" />
</sourceFieldsMap>
<sourceTables>
<table>eye1</table>
<table>eye2</table>
</sourceTables>
</dataServiceResource>
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Baidu {
public static void main(String[] args) throws Exception {
List<String> list = display();
Map<String, String> map = run(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i) + "--" +map.get(list.get(i)));
}
}
private static String path;
static {
path = Baidu.class.getResource("/test.xml").getPath();
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
// 得到normativeField中每项的值
public static List<String> display() throws Exception{
Document doc = getDocument(path);
NodeList list = doc.getElementsByTagName("normativeField");
List<String> normativeFieldList = new ArrayList<String>();
// 得到normativeField中每项的值加到normativeFieldList集合里
for (int i = 0; i < list.getLength(); i++) {
normativeFieldList.add(list.item(i).getTextContent());
}
return normativeFieldList;
}
public static Map<String, String> run(List<String> list) throws Exception {
Map<String, String> map = new HashMap<String, String>();
Document doc = getDocument(path);
for (int i = 0; i < list.size(); i++) {
NodeList nodeListlist = doc.getElementsByTagName(list.get(i));
Element e = (Element) nodeListlist.item(0);
String value = e.getAttribute("value");
map.put(list.get(i), value);
}
return map;
}
public static Document getDocument(String path) throws Exception {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(path);
return doc;
}
}
控制台:
ID--eye1.id
xingming--eye1.name
xingbie--eye1.sex
nianling--eye1.age
shili--eye2.eyelight
shengao--eye2.high
tizhong--eye2.wealth
guomin--
gongfeizifei--eye1.pay
lianxifangshi--eye1.telephonenumber
bingli--eye1.content1
test.xml要在工程根目录下:
<?xml version="1.0" encoding="UTF-8"?>
<dataServiceResource>
<databasetype>relation</databasetype>
<normativeResource>
<normativeField>ID</normativeField>
<normativeField>xingming</normativeField>
<normativeField>xingbie</normativeField>
<normativeField>nianling</normativeField>
<normativeField>shili</normativeField>
<normativeField>shengao</normativeField>
<normativeField>tizhong</normativeField>
<normativeField>guomin</normativeField>
<normativeField>gongfeizifei</normativeField>
<normativeField>lianxifangshi</normativeField>
<normativeField>bingli</normativeField>
</normativeResource>
<sourceFieldsMap>
<ID value="eye1.id" />
<xingming value="eye1.name" />
<xingbie value="eye1.sex" />
<nianling value="eye1.age" />
<shili value="eye2.eyelight" />
<shengao value="eye2.high" />
<tizhong value="eye2.wealth" />
<guomin value="" />
<gongfeizifei value="eye1.pay" />
<lianxifangshi value="eye1.telephonenumber" />
<bingli value="eye1.content1" />
</sourceFieldsMap>
<sourceTables>
<table>eye1</table>
<table>eye2</table>
</sourceTables>
</dataServiceResource>
展开全部
SAXReader reader = new SAXReader();// 实例化解析器
File file = new File("路径");
Document doc = reader.read(file);
Element root = doc.getRootElement();// 得到根节点
List childList = root.elements(); //获取所有子节点
Element rsElement = root.element("rs");//获取rs节点
//获取子节点属性名、值
List attrList = root.attributes();
for (int i = 0; i < attrList.size(); i++) {
Attribute item = (Attribute)attrList.get(i);
System.out.println(item.getName() + "=" + item.getValue());
}
File file = new File("路径");
Document doc = reader.read(file);
Element root = doc.getRootElement();// 得到根节点
List childList = root.elements(); //获取所有子节点
Element rsElement = root.element("rs");//获取rs节点
//获取子节点属性名、值
List attrList = root.attributes();
for (int i = 0; i < attrList.size(); i++) {
Attribute item = (Attribute)attrList.get(i);
System.out.println(item.getName() + "=" + item.getValue());
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SAXReader reader = new SAXReader();// 实例化解析器
File file = new File("路径");
Document doc = reader.read(file);
Element root = doc.getRootElement();// 得到根节点
其他的,自己去参考吧,不是太难,相信你可以的!
File file = new File("路径");
Document doc = reader.read(file);
Element root = doc.getRootElement();// 得到根节点
其他的,自己去参考吧,不是太难,相信你可以的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询