0

这是我的 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。

4

2 回答 2

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");
于 2012-10-10T09:57:22.970 回答
0

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

String id=e.getAttribute("id");
String date=e.getAttribute("date");
于 2012-10-10T09:56:38.660 回答