java - Dom parser is not working- output shown is all null -
i want parse xml file called "weather.xml" shown below:
<?xml version ="1.0" encoding="utf-8" ?> <weather> <forecast_information> <city data="pittsford, ny" /> <postal_code data="14534" /> <forecast_date data="2015-03-12" /> <unit_system data="us" /> <condition data = "mostly cloudy" /> <temp_f data ="42" /> <wind_condition data="wind: nw @ 7 mph" /> <day_of_week data="sat" /> <low data="32"/> <high data = "45" /> <condition data="rain , snow" /> </forecast_information> <forecast_information> <city data= "rochester, ny" /> <postal_code data="14623" /> <forecast_date data= "2015-03-12" /> <unit_system data="us" /> <condition data="partly cloudy" /> <temp_f data="40" /> <wind_condition data="wind: st @ 3.5 mph" /> <day_of_week data="mon" /> <low data="30" /> <high data="40" /> <condition data="bright , sunny" /> </forecast_information> </weather>
i have coded dom parser in following way:
public class domparserdemo { public static void main(string[] args){ try { file inputfile = new file("weather.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(inputfile); doc.getdocumentelement().normalize(); system.out.println("root element :" + doc.getdocumentelement().getnodename()); nodelist nlist = doc.getelementsbytagname("forecast_information"); system.out.println("----------------------------"); (int temp = 0; temp < nlist.getlength(); temp++) { node nnode = nlist.item(temp); system.out.println("\ncurrent element :" + nnode.getnodename()); if (nnode.getnodetype() == node.element_node) { element eelement = (element) nnode; system.out.println("city : " + eelement.getattribute("city")); system.out.println("postal_code : " + eelement.getelementsbytagname("postal_code").item(0).gettextcontent()); system.out.println("forecast date : " + eelement.getelementsbytagname("forecast_date").item(0).gettextcontent()); system.out.println("unit system : " + eelement .getelementsbytagname("unit_system") .item(0).gettextcontent()); system.out.println("condition : " + eelement .getelementsbytagname("condition") .item(0).gettextcontent()); system.out.println("wind condition : " + eelement .getelementsbytagname("wind_condition") .item(0).gettextcontent()); system.out.println("day of week : " + eelement .getelementsbytagname("day_of_week") .item(0).gettextcontent()); system.out.println("low : " + eelement .getelementsbytagname("low") .item(0).gettextcontent()); system.out.println("high: " + eelement .getelementsbytagname("high") .item(0).gettextcontent()); } } } catch (exception e) { e.printstacktrace(); } } }
but output shown follows, information not being extracted .xml file provided:
root element :weather ---------------------------- current element :forecast_information city : postal_code : forecast date : unit system : condition : wind condition : day of week : low : high: current element :forecast_information city : postal_code : forecast date : unit system : condition : wind condition : day of week : low : high:
first mistake:
system.out.println("city : " + eelement.getattribute("city"));
you asking attribute of element named city
, forecast_information
element doesn't have attributes. city
child element of forecast_information
, should extract either traversing children or using getelementsbytagname()
rest of items.
second mistake:
for other elements, doing getelementsbytagname
, works fine retrieving element itself. however, data
item want retrieve not content - it's attribute.
using gettextcontent()
correct if items looked this:
<forecast_date>2015-03-12</forecast_date> <unit_system>us</unit_system>
but not how xml formatted. in xml, need retrieve attribute data
. should replace calls like:
system.out.println("postal_code : " + eelement.getelementsbytagname("postal_code").item(0).gettextcontent());
to:
system.out.println("postal_code : " + eelement.getelementsbytagname("postal_code").item(0).getattribute("data"));
Comments
Post a Comment