jsp解析xml代碼的簡單介紹

本文目錄一覽:

用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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BTJET的頭像BTJET
上一篇 2025-01-09 12:15
下一篇 2025-01-09 12:15

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29

發表回復

登錄後才能評論