how to store xml data in to arraylist java -
i have xml file data (not complete xml file)
<header> <parametercontext> <parameter> <name>fileid<name> <value>1001445<value> </parameter> <identifier>id</identifier> </parametercontext> <parametercontext> <parameter> <name>product</name> <value>ect</ns0:value> </parameter> <identifier>productname</identifier> </parametercontext> </header>
please me store xml data(data of parametercontext elements) in arraylist.
sorry not posting earlier. have 2 classes object , load
public class object{ private arraylist<parametercontext> parameterctx = new arraylist<parametercontext>(); public arraylist<parametercontext> getparameterctx() { return parameterctx; } public void setparameterctx(parametercontext parameterctx) { this.parameterctx.add(parameterctx) ; } public parameter searchparameter(string name, string identifier, arraylist<parametercontext> al) { for(int = 0; < al.size(); i++) { if(al.get(i).getidentifier().equalsignorecase(identifier)) { for(int j = 0; j < al.get(i).getparameter().size(); j++) { if(al.get(i).getparameter().get(j).getname().equalsignorecase(name) ) { return al.get(i).getparameter().get(j); } } } } return null ; }
}
and
public class load{ @xmlelement(name = "parametercontext") protected list<load.parametercontext> parametercontext; public list<load.parametercontext> getparametercontext() { if (parametercontext == null) { parametercontext = new arraylist<load.parametercontext>(); } return this.parametercontext; } @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { "parameter", "identifier" }) public static class parametercontext { @xmlelement(name = "parameter", required = true) protected list<load.parametercontext.parameter> parameter; @xmlelement(name = "identifier", required = true) protected string identifier; public list<load.parametercontext.parameter> getparameter() { if (parameter == null) { parameter = new arraylist<load.parametercontext.parameter>(); } return this.parameter; } public string getidentifier() { return identifier; } public void setidentifier(string value) { this.identifier = value; } @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { "name", "value" }) public static class parameter { @xmlelement(name = "name", required = true) protected string name; @xmlelement(name = "value", required = true) protected string value; public string getname() { return name; } public void setname(string value) { this.name = value; } public string getvalue() { return value; } public void setvalue(string value) { this.value = value; }
when use object.searchparameter("field","id",object.getparameterctx()).getvalue());
nullpointerexception coming.(getparameterctx not initialized).before line need initialize right.how initialize?
using jaxb, create class mimics xml
@xmlrootelement(name="header") public class xmlbean { public list<parametercontext> pc; public xmlbean() { } public list<parametercontext> getpc() { return pc; } public void setpc(list<parametercontext> pc) { this.pc = pc; } } public class parametercontext { public parameter parameter; public string identifier; public parametercontext() { } public parameter getparameter() { return pc; } public void setparameter(parameter parameter) { this.param = param; } public string getidentifier() { return identifer; } public void setidentifier(string identifier) { this.identifier = identifier; } } public class parameter { public string name; public string value; public parameter() { } public void setname(string name) { this.name = name; } public void getname() { return name; } public void setvalue(string value) { this.value = value; } public void getvalue(string value) { return value; } } public static void main(string[] args) { inputstream = yourxml jaxbcontext jaxb = jaxbcontext.newinstace(xmlbean.class); unmarshaller jaxbunmarshaller = jaxb.createunmarshaller(); xmlbean xml = (xmlbean) jaxbunmarshaller.unmarshal(is); system.out.println(xml.getparametercontext().get(0).getparam().getname()); system.out.println(xml.getparametercontext().get(0).getparam().getvalue()); }
something that
Comments
Post a Comment