如何在给定的XML中打印日期和ID

时间:2012-10-10 09:43:44

标签: java android

这是我的xml格式:

<taxmann>
    <docdetails>
        <info id="104010000000006516" date="20120120">
            <physicalpath>\\192.168.1.102\CMS\DATA</physicalpath>
            <filepath isxml="N">\CIRCULARS\DIRECTTAXLAWS\HTMLFILES\CIRDGBACDD4836150012011122012012.htm</filepath>
            <summary></summary>
            <description></description>
            <heading>DGBA.CDD. NO.H- 4836 /15.02.001/2011-12 | Clarification on Regulation of Interest Rates for Small Savings Schemes</heading>
            <correspondingcitation/>
            <hasfile>YES</hasfile>
            <sortby>20120328155728957</sortby>
            <parentid></parentid>
            <parentchapterid></parentchapterid>
        </info>
    </docdetails>
</taxmann>

我能够检索标题的数据,但我也想打印日期和ID,但我无法做到这一点。请告诉我如何实施它。

XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(url); // getting XML

    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;

    for (int i = indexRowStart; i < indexRowEnd; i++) {
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map = new HashMap<String, String>();
        map.put("RowID", String.valueOf(RowID));

        String Heading= parser.getValue(e, KEY_NAME).replace("|", "|\n").replace("|", "");
        map.put(KEY_NAME,Heading);

        // adding HashList to ArrayList
        menuItems.add(map);
}

这是我的代码,请告诉我我可以解析的逻辑,以便我也可以获得日期和ID。

2 个答案:

答案 0 :(得分:3)

您确定这是阅读该xml文件的最简单方法吗?它看起来有点太复杂了。为什么不手动导航树结构?

我会说你会这样得到它:

SAXBuilder builder = new SAXBuilder();     
Document doc;
doc = builder.build(file);

//rootElement would be your "taxmann" element
Element rootElement =   doc.getRootElement();
Element docdetailsElement = rootElement.getChild("docdetails");
Element infoElement = docdetailsElement.getChild("info");

String id = infoElement.getAttributeValue("id");
String date = infoElement.getAttributeValue("date");

答案 1 :(得分:0)

您应该使用Element#getAttribute(String name)方法。在你的情况下像:

String id=e.getAttribute("id");
String date=e.getAttribute("date");