dom4j解析xml文件,将内容显示在jsp如何做?

dom4j解析xml文件,将内容显示在jsp如何做?,第1张

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 = 0i <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 = 0i <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 = 0i <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>

很简单的,给你现成的代码参考。

String a = "<name>javk</name>"

out.println(a) 

a=a.replace("<","&lt").replace(">","&gt")

out.println(a)

1-apache的POI,此方法对Excel的导出做的很好,目前对Word的导出方面的功能尚未完全。

2-纯JavaScript脚本实现。主要通过客户端调用本机Office组件来实现。

3-在JSP页面引入头文件实现。

纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject('Word.Application'),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安全级别设置为中。这样的话岂不是每台机器都要配置一下。其次每次生成word文档以后d出对话框(无法保存此文件,因为它已在别处打开(C:\...\STARTUP\Powerword.dot)),出现此问题就需要把C:\Documents and Settings\当前用户名\Application Data\Microsoft\Word\STARTUP下的Powerword.dot文件删除,每次遇到此问题就需要删除文件来解决,十分不方便。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11686928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存