java 读取xml文件中所有item

<menu><!--Server--><itemtype="tab"id="tab-server"label="tab.server"url="index.jsp"des... <menu>
<!-- Server -->
<item type="tab" id="tab-server" label="tab.server" url="index.jsp" description="tab.server.descr">
<children>
<!-- Server Manager -->
<item type="sidebar" id="sidebar-server-manager" label="sidebar.server-manager">
<children>

<!-- Server Settings -->
<item id="server-settings" label="sidebar.server-settings" url="index.jsp" description="sidebar.server-settings.descr"/>

<!-- System Properties -->
<item id="server-props" label="sidebar.system-props" url="server-properties.jsp" description="sidebar.system-props.descr"/>

<!-- backup server -->

<item id="server-outservicemanager" label="sidebar.server-outservicemanager" url="outservice.action" description="sidebar.server-outservicemanager"/>
<!-- Server to server -->
<!-- -->
<item id="server2server-settings" label="sidebar.server2server-settings" url="server2server-settings.jsp" description="sidebar.server2server-settings.descr"/>

<!-- External components -->
<item id="connection-managers-settings" label="sidebar.connection-managers-settings" url="connection-managers-settings.jsp" description="sidebar.connection-managers-settings.descr"/>

<item id="sidebar-multiserver" label="sidebar.multiserver" url="settingMultiServer.action" description="sidebar.multiserver"/>

<!-- System Locale -->
<item id="server-locale" label="sidebar.server-locale" url="server-locale.jsp" description="sidebar.server-locale.descr"/>

<!-- license manager -->
<item id="license-settings" label="sidebar.license-config" url="licenseConfig.jsp" description="sidebar.license-config.descr"/>

</children>
</item>
</children>
</item>
展开
 我来答
corruptsatan
2010-04-21 · TA获得超过1108个赞
知道小有建树答主
回答量:473
采纳率:0%
帮助的人:618万
展开全部
例子是将XML保存在D盘根目录下,名字为:test.xml

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

class Item{
private String id;
private String type;
private String label;
private String url;
private String description;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("item: ");

if(getId()!=null && !"".equals(getId())){
sb.append("id = ").append(getId()).append(",");
}
if(getType()!=null && !"".equals(getType())){
sb.append("type = ").append(getType()).append(",");
}
if(getLabel()!=null && !"".equals(getLabel())){
sb.append("label = ").append(getLabel()).append(",");
}
if(getUrl()!=null && !"".equals(getUrl())){
sb.append("url = ").append(getUrl()).append(",");
}
if(getDescription()!=null && !"".equals(getDescription())){
sb.append("description = ").append(getDescription()).append(",");
}

return sb.toString();
}

}

public class TestXML {

/**
*
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

List<Item> itemList = new ArrayList<Item>();

DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse("D:\\test.xml");

NodeList nodeList = doc.getElementsByTagName("item");

for(int i=0; i<nodeList.getLength(); i++){
Node node = nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element el = (Element) node;

Item item = new Item();

item.setId(el.getAttribute("id"));
item.setType(el.getAttribute("type"));
item.setLabel(el.getAttribute("label"));
item.setUrl(el.getAttribute("url"));
item.setDescription(el.getAttribute("description"));

itemList.add(item);
}
}
System.out.println("------------");
for(Item item:itemList){
System.out.println(item);
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
80htm
2010-04-21 · TA获得超过712个赞
知道小有建树答主
回答量:74
采纳率:0%
帮助的人:96.7万
展开全部
我E盘放了这个xml文件

public class Sovle1 {

public static void main(String args[]) throws ParseException, ParserConfigurationException, SAXException, IOException{
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse("e:/test.xml");
NodeList list = document.getElementsByTagName("item");
for(int i=0; i<list.getLength(); i++){
Node node = list.item(i);
System.out.println(node.getAttributes().getNamedItem("label"));
}

}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式