Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<weather>

<current_conditions>

<temp_f data="34" />

</current_conditions>

</weather>

How can i read this xml file in java.

EDIT(Copied from Comment by OP)


C#
public static void gh()
{
	 try {
		 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		    DocumentBuilder db =dbf.newDocumentBuilder();
		    Document doc=db.parse("G:/books.xml");
		    
		    
		    NodeList listOfPersons = doc.getElementsByTagName("current_conditions");
		    int totalPersons = listOfPersons.getLength();
            System.out.println(""+ totalPersons);

            for(int s=0; s<listOfPersons.getLength() ; s++){


                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                    Element firstPersonElement = (Element)firstPersonNode;

                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("temp_c");
                    Element firstNameElement = (Element)firstNameList.item(0);

                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out.println("First Name : " + 
                           ((Node)textFNList.item(0)).getNodeValue().trim());
                }
            }
		  } catch (Exception e) {
		    e.printStackTrace();
		  }
		 }

This is my code
Posted
Updated 26-Nov-11 21:29pm
v3

1 solution

 
Share this answer
 
Comments
thanga__raj 27-Nov-11 2:09am    
I already read this link.But i want to know is how to read that temp_f data
tag.If i read this java.lang.nullpointer Exception error is occur.
Mehdi Gholam 27-Nov-11 2:21am    
Show your code.
thanga__raj 27-Nov-11 3:00am    
public static void gh()
{
try {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document doc=db.parse("G:/books.xml");


NodeList listOfPersons = doc.getElementsByTagName("current_conditions");
int totalPersons = listOfPersons.getLength();
System.out.println(""+ totalPersons);

for(int s=0; s<listOfPersons.getLength() ; s++){


Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


Element firstPersonElement = (Element)firstPersonNode;

//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("temp_c");
Element firstNameElement = (Element)firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
This is my code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900