java - How to extract attribute values with XPath -


i have following xml document , want transform station element attribute values in java object:

<locvalres flag="final" id="dummy">     <station name="sampieri" distance="2901857" duration="3482348" externalid="008302686#80" externalstationnr="008302686" type="wgs84" x="14744601" y="36731901"/>     <station name="pozzallo" distance="2903750" duration="3484620" externalid="008302642#80" externalstationnr="008302642" type="wgs84" x="14847168" y="36733609"/>     <station name="scicli" distance="2907477" duration="3489092" externalid="008302695#80" externalstationnr="008302695" type="wgs84" x="14699187" y="36790088"/>     <station name="ispica" distance="2909739" duration="3491806" externalid="008302562#80" externalstationnr="008302562" type="wgs84" x="14914362" y="36778043"/>     <station name="rosolini" distance="2914534" duration="3497560" externalid="008302660#80" externalstationnr="008302660" type="wgs84" x="14952836" y="36815950"/>     <station name="modica" distance="2915249" duration="3498418" externalid="008300462#80" externalstationnr="008300462" type="wgs84" x="14754166" y="36852950"/>     <station name="donnafugata" distance="2915468" duration="3498681" externalid="008302530#80" externalstationnr="008302530" type="wgs84" x="14568817" y="36882183"/>     <station name="ragusa" distance="2922026" duration="3506551" externalid="008300336#80" externalstationnr="008300336" type="wgs84" x="14725427" y="36919209"/>     <station name="comiso" distance="2923096" duration="3507835" externalid="008302524#80" externalstationnr="008302524" type="wgs84" x="14600540" y="36947283"/>     <station name="vittoria" distance="2923168" duration="3507921" externalid="008300491#80" externalstationnr="008300491" type="wgs84" x="14524959" y="36958762"/>     <station name="noto" distance="2923895" duration="3508794" externalid="008302609#80" externalstationnr="008302609" type="wgs84" x="15074307" y="36882498"/>     <station name="acate" distance="2925789" duration="3511066" externalid="008302445#80" externalstationnr="008302445" type="wgs84" x="14425430" y="36996652"/>     <station name="avola" distance="2927532" duration="3513158" externalid="008302461#80" externalstationnr="008302461" type="wgs84" x="15125762" y="36907586"/>     <station name="gela-anic" distance="2929204" duration="3515164" externalid="008302743#80" externalstationnr="008302743" type="wgs84" x="14299176" y="37045013"/>     <station name="licata" distance="2930810" duration="3517092" externalid="008300380#80" externalstationnr="008300380" type="wgs84" x="13938278" y="37105987"/>     <station name="gela" distance="2931688" duration="3518145" externalid="008300310#80" externalstationnr="008300310" type="wgs84" x="14259255" y="37072988"/>     <station name="falconara" distance="2933198" duration="3519957" externalid="008302532#80" externalstationnr="008302532" type="wgs84" x="14052468" y="37113511"/>     <station name="niscemi" distance="2941720" duration="3530184" externalid="008300423#80" externalstationnr="008300423" type="wgs84" x="14403505" y="37144992"/>     <station name="siracusa" distance="2947889" duration="3537586" externalid="008300338#80" externalstationnr="008300338" type="wgs84" x="15281733" y="37068907"/>     <station name="campobello ravanusa" distance="2948468" duration="3538281" externalid="008302485#80" externalstationnr="008302485" type="wgs84" x="13946350" y="37265582"/> </locvalres> 

i mean need each station xml element own java object. no problem, have solution task. want extract each attribute value xpath. have written following java code:

 private static string getattributvalue(string attribute, string xmlresponsebody) {      string searchattributevalue = "";     try {         documentbuilderfactory docbuilderfactory = documentbuilderfactory.newinstance();         documentbuilder docbuilder = docbuilderfactory.newdocumentbuilder();         document doc = docbuilder.parse(new inputsource(new stringreader(xmlresponsebody)));         xpathfactory xpathfactory = xpathfactory.newinstance();         xpath xpath = xpathfactory.newxpath();         try {             xpathexpression expr = xpath.compile("//@" + attribute);             object result = expr.evaluate(doc, xpathconstants.nodeset);             nodelist nodelist = (nodelist) result;             (int = 0; < nodelist.getlength(); i++) {                 searchattributevalue = nodelist.item(i).gettextcontent();             }         } catch (xpathexpressionexception e) {             e.printstacktrace();         }     } catch (parserconfigurationexception pce) {         pce.printstacktrace();     } catch (ioexception ioe) {         ioe.printstacktrace();     } catch (saxexception sae) {         sae.printstacktrace();     }     return searchattributevalue; } 

but loop ...

for (int = 0; < nodelist.getlength(); i++) {             searchattributevalue = nodelist.item(i).gettextcontent();         } 

... extract value last station element. think need position prädicate in xpath expression, maybe quite similar this:

//@" + attribute + [position() + +] 

how can extract each attribute value of each available station element in java loop code?

assuming rest of code working, instead of returning single result, return either array or list of values...

private static list<string> getattributvalue(string attribute, string xmlresponsebody) {     list<string> results = new arraylist<>(25);     string searchattributevalue = "";     try {     //...//         try {         //...//             nodelist nodelist = (nodelist) result;             (int = 0; < nodelist.getlength(); i++) {                 results.add(nodelist.item(i).gettextcontent());             }         } catch (xpathexpressionexception e) {             e.printstacktrace();         }     } catch (parserconfigurationexception pce) {         pce.printstacktrace();     } catch (ioexception ioe) {         ioe.printstacktrace();     } catch (saxexception sae) {         sae.printstacktrace();     }     return results; } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -