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> 展开
<!-- 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> 展开
2个回答
展开全部
例子是将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);
}
}
}
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);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我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"));
}
}
}
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"));
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询