本文目录一览:
- 1、用JSP解析XML
- 2、JSP解析XML文件
- 3、紧急求救!!!在jsp中解析xml时,代码出现错误,请问我应该导入哪些jar包,能使程序正常,截图如下
- 4、JSP中能用javascript解析xml吗?(我知道可以直接用java解析) 为什么我做的时候老是说缺少对象?
用JSP解析XML
在后面用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 {
ListString list = display();
MapString, 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 ListString display() throws Exception{
Document doc = getDocument(path);
NodeList list = doc.getElementsByTagName("normativeField");
ListString normativeFieldList = new ArrayListString();
// 得到normativeField中每项的值加到normativeFieldList集合里
for (int i = 0; i list.getLength(); i++) {
normativeFieldList.add(list.item(i).getTextContent());
}
return normativeFieldList;
}
public static MapString, String run(ListString list) throws Exception {
MapString, String map = new HashMapString, 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
databasetyperelation/databasetype
normativeResource
normativeFieldID/normativeField
normativeFieldxingming/normativeField
normativeFieldxingbie/normativeField
normativeFieldnianling/normativeField
normativeFieldshili/normativeField
normativeFieldshengao/normativeField
normativeFieldtizhong/normativeField
normativeFieldguomin/normativeField
normativeFieldgongfeizifei/normativeField
normativeFieldlianxifangshi/normativeField
normativeFieldbingli/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
tableeye1/table
tableeye2/table
/sourceTables
/dataServiceResource
JSP解析XML文件
把你的xml文档贴出来看下。。。是不是你的nodelist就没取到东西?
紧急求救!!!在jsp中解析xml时,代码出现错误,请问我应该导入哪些jar包,能使程序正常,截图如下
我可以帮你解决,我昨天刚把这个实现了。你留QQ给我。我加你。然后我把jar 给你。你自己复制进去。然后引入一下就可以了
JSP中能用javascript解析xml吗?(我知道可以直接用java解析) 为什么我做的时候老是说缺少对象?
IE下可以直接使用LoadXML方法解析XML字符串,而在FF下则要使用DOMParser 对象的parseFromString() 方法即
var oParser=new DOMParser();
xmlDoc=oParser.parseFromString(xmlStr,"text/xml");
var document_xml=new ActiveXObject("Microsoft.XMLDOM");
document_xml.loadXML(XML);
var name=document_xml.getElementsByTagName("vcOne");
var value=document_xml.getElementsByTagName("vcAction ");
var str1=name(0).firstChild.nodeValue;
一些关键的步骤 这样你应该能完成剩下的全部吧
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela