如何用java获得xml文件中标签的属性列表值?
1.有xmlcount.xml文件。文件的内容如下:<?xmlversion="1.0"encoding="UTF-8"?><count-object-...
1.有xmlcount.xml文件。文件的内容如下:<?xml version="1.0" encoding="UTF-8"?><count-object-inf month-count="34" total-count="34" temp-count="0" week-count="34" day-count="16" year-count="34"><date>2010-11-26T11:30:10.234+08:00</date></count-object-inf>现在我想通过java去读取xmlcount.xml文件,然后获得其中标签名为:count-object-inf的多个属性,请问怎么做?
展开
2个回答
展开全部
用jdom.jar
import java.io.File;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class Test {
public static void main(String[] args)throws Exception {
File file = new File("c:/xmlcount.xml");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(file);
Element root =doc.getRootElement();
System.out.println("month-count:"+root.getAttributeValue("month-count"));
System.out.println("total-count:"+root.getAttributeValue("total-count"));
System.out.println("temp-count:"+root.getAttributeValue("temp-count"));
System.out.println("week-count:"+root.getAttributeValue("week-count"));
System.out.println("day-count:"+root.getAttributeValue("day-count"));
System.out.println("year-count:"+root.getAttributeValue("year-count"));
}
}
import java.io.File;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class Test {
public static void main(String[] args)throws Exception {
File file = new File("c:/xmlcount.xml");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(file);
Element root =doc.getRootElement();
System.out.println("month-count:"+root.getAttributeValue("month-count"));
System.out.println("total-count:"+root.getAttributeValue("total-count"));
System.out.println("temp-count:"+root.getAttributeValue("temp-count"));
System.out.println("week-count:"+root.getAttributeValue("week-count"));
System.out.println("day-count:"+root.getAttributeValue("day-count"));
System.out.println("year-count:"+root.getAttributeValue("year-count"));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询