本文目錄一覽:
- 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
原創文章,作者:BTJET,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316916.html