XML Parsing. Trying to get attributes

kasiko

New Member
I'm developing a java class to parse this xml file:\[code\]<document src="http://stackoverflow.com/questions/15896934/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="http://stackoverflow.com/questions/15896934/productview/info.html"></url></module></document>\[/code\]And I've this java class:\[code\]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();}\[/code\]As you can see, it's looping for every "module" tag and getting its child value. But I need name attribute from module, but as It's a NodeList, can't use method \[code\]getAttribute("name");\[/code\]Any idea?
 
Back
Top