1

このxmlファイルを解析するJavaクラスを開発しています:

<document src="xmls/sections/modules/200_1.xml">
<module name="product_info" id="1">
    <product_primary_id>200</product_primary_id>
    <product_section_id>1</product_section_id>
    <product_section_item_id></product_section_item_id>

    <type>1</type>
    <position>1</position>
    <align>top</align>

    <url href="productview/info.html"></url>
</module>
</document>

そして、私はこのJavaクラスを持っています:

try {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(contingut);
    doc.getDocumentElement().normalize();
    //loop a cada module
    NodeList nodes = doc.getElementsByTagName("module");
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        Element element = (Element) node;
        if(element.getNodeType() == Element.ELEMENT_NODE){
            Log.d("debugging","product_primary_id: "+getValue("product_primary_id",element));
            Log.d("debugging","product_section_id: "+getValue("product_section_id",element));
            //Log.d("debugging","product_section_item_id: "+getValue("product_section_item_id",element));
            Log.d("debugging","type: "+getValue("type",element));
            Log.d("debugging","position: "+getValue("position",element));
            Log.d("debugging","align: "+getValue("align",element));
            //Log.d("debugging","url: "+getValue("url",element));   
        }
    }
} catch (Exception e){
            e.printStackTrace();
}

ご覧のとおり、すべての「モジュール」タグに対してループし、その子の値を取得しています。しかし、モジュールから name 属性が必要ですが、それは NodeList であるため、メソッドを使用できませんgetAttribute("name");

何か案が?

4

2 に答える 2

2

あなたはただすることができます

Element element = (Element) node;
element.getAttribute("name")

タグnameを表すノードの属性を取得します。タグ自体も要素なmoduleので、そうすることができます。メソッドはここに文書化moduleれていますgetAttribute()

于 2013-04-09T08:37:56.647 に答える
1
doc.getElementsByTagName("module")

指定されたタグ名、つまりモジュールを持つ要素のリストを返します。ノードのリストを反復処理する場合は、for ステートメント内でgetAttribute関数を使用する必要があります。

http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html#getElementsByTagName%28java.lang.String%29を参照してください。

于 2013-04-09T08:37:26.150 に答える