xml - How to parse xsd String to fetch its elements using java? -


i need solution on "how parse xml file , xsd element's values"?

as result of 1 of web service method call, java object contains xsd java string. xsd contains many xsd:elements. need fetch 1 of element called "accountname" contains list of accountnames in it. need use java code here fetch , store 1 list. here is:

metadataxsd -----------> <xsd:schema xmlns:xsd="http://www.w3.org    /2001/xmlschema">    <xsd:element name="metadata">     <xsd:complextype>   <xsd:sequence>     <xsd:element id="md_37ee5de8-996b-0371-a119-61b77fe6abcf" name="category" minoccurs="0" maxoccurs="1">       <xsd:annotation>         <xsd:documentation/>         <xsd:appinfo>           <label>category</label>           <key>category</key>           <searchable>true</searchable>           <timecontrol>false</timecontrol>           <description>specify categories media content should published</description>         </xsd:appinfo>       </xsd:annotation>       <xsd:simpletype>         <xsd:restriction base="listtype">           <xsd:enumeration value="industries &amp; solutions"/>           <xsd:enumeration value="industries &amp; solutions - solutions topics"/>           <xsd:enumeration value="industries &amp; solutions - industry solutions"/>           <xsd:enumeration value="industries &amp; solutions - business partners , alliances"/>           <xsd:enumeration value="services"/>           <xsd:enumeration value="services - business services"/>           <xsd:enumeration value="services - services"/>           <xsd:enumeration value="services - outsourcing services"/>           <xsd:enumeration value="services - training"/>           <xsd:enumeration value="services - additional services"/>           <xsd:enumeration value="products"/>           <xsd:enumeration value="products - software"/>           <xsd:enumeration value="products - systems"/>           <xsd:enumeration value="products - storage"/>           <xsd:enumeration value="products - additional products"/>           <xsd:enumeration value="support &amp; downloads"/>           <xsd:enumeration value="support &amp; downloads - knowledge center"/>           <xsd:enumeration value="support &amp; downloads - knowledge center - education assistant"/>           <xsd:enumeration value="support &amp; downloads - technical support"/>           <xsd:enumeration value="support &amp; downloads - developer support"/>           <xsd:enumeration value="support &amp; downloads - supplier support"/>           <xsd:enumeration value="support &amp; downloads - former products"/>           <xsd:enumeration value="support &amp; downloads - downloads"/>           <xsd:enumeration value="my"/>           <xsd:enumeration value="interests"/>         </xsd:restriction>       </xsd:simpletype>     </xsd:element>     <xsd:element id="md_acecfc0e-1f3b-8233-17b3-aeb96af9d030" name="accountname" minoccurs="0" maxoccurs="1">       <xsd:annotation>         <xsd:documentation></xsd:documentation>         <xsd:appinfo>           <label>account name</label>           <key>account name</key>           <searchable>true</searchable>           <timecontrol>false</timecontrol>           <description>specify account owns media content</description>         </xsd:appinfo>       </xsd:annotation>       <xsd:simpletype>         <xsd:restriction base="listtype">           <xsd:enumeration value="analytics business unit client success sales eminence"/>           <xsd:enumeration value="digital"/>           <xsd:enumeration value="enterprise media team enablement"/>           <xsd:enumeration value="enterprise content , esupport services"/>           <xsd:enumeration value="global delivery center"/>           <xsd:enumeration value="digital video services new york"/>           <xsd:enumeration value="enterprise social solutions"/>           <xsd:enumeration value="gbs knowledge"/>           <xsd:enumeration value="gps technology"/>           <xsd:enumeration value="global private digital commercde"/>           <xsd:enumeration value="center"/>           <xsd:enumeration value="mix"/>           <xsd:enumeration value="essentials"/>           <xsd:enumeration value="references"/>           <xsd:enumeration value="learning services"/>           <xsd:enumeration value="works"/>           <xsd:enumeration value="programs &amp; operations"/>           <xsd:enumeration value="ibm digital – chq marketing"/>           <xsd:enumeration value="assistant"/>           <xsd:enumeration value="content catalog"/>           <xsd:enumeration value="center"/>           <xsd:enumeration value="books"/>           <xsd:enumeration value="ser"/>           <xsd:enumeration value="marketing , communications"/>           <xsd:enumeration value="middleware client success"/>           <xsd:enumeration value="pw"/>           <xsd:enumeration value="enablement"/>           <xsd:enumeration value="sales"/>           <xsd:enumeration value="systems unit"/>           <xsd:enumeration value="technical"/>         </xsd:restriction>       </xsd:simpletype>     </xsd:element>     </xsd:schema> 

from above need fetch accountname element , store of values 1 list , further process.

i tried below code converts string xml.

metadataxsd = metadataprofile.xsd;                     if(metadataxsd != null) {                         system.out.println("metadataxsd ----------->"+metadataxsd);                             finalxmldoc = stringtodom(metadataxsd);                             system.out.println("finalxmldoc ----------->"+finalxmldoc);                         }                     }  public static document stringtodom(string xmlsource) throws saxexception, parserconfigurationexception, ioexception, transformerexception{         documentbuilderfactory factory = documentbuilderfactory.newinstance();           documentbuilder builder;           try          {               builder = factory.newdocumentbuilder();               document doc = builder.parse( new inputsource(new stringreader(xmlsource)));              transformerfactory tranfactory = transformerfactory.newinstance();             transformer atransformer = tranfactory.newtransformer();             source src = new domsource( doc );             result dest = new streamresult( new file( "c:/accountnames.xml" ) );             system.out.println("destination xml -------->"+dest);             atransformer.transform( src, dest );             document doc1 = builder.parse (new file("c:/accountnames.xml"));              nodelist list = doc1.getelementsbytagname("xsd:element");               for(int = 0 ; < list.getlength(); i++)             {                 element first = (element)list.item(i);               }             return doc;         } catch (exception e) {               e.printstacktrace();           }          return null;     } 

now need values java list. please me resolving this.

here solution problem. hope helps somebody.

public static document stringtodom(string xmlsource) throws saxexception, parserconfigurationexception, ioexception, transformerexception{         documentbuilderfactory factory = documentbuilderfactory.newinstance();           documentbuilder builder;           nodelist nodelist = null;         list<string> accountnameslist = new arraylist<string>();         try          {               builder = factory.newdocumentbuilder();               document doc = builder.parse(new inputsource(new stringreader(xmlsource)));              transformerfactory tranfactory = transformerfactory.newinstance();             transformer atransformer = tranfactory.newtransformer();             source src = new domsource(doc);             result dest = new streamresult(new file("c:/accountnames.xml" ) );             atransformer.transform( src, dest );             document doc1 = builder.parse (new file("c:/accountnames.xml"));              nodelist list = doc1.getelementsbytagname("xsd:element");             for(int = 0 ; < list.getlength(); i++)             {                 element first = (element)list.item(i);                 if(first.hasattributes()) {                     string attrname = first.getattribute("name");                     if(attrname.equalsignorecase("accountname")) {                         nodelist list1 = first.getelementsbytagname("xsd:enumeration");                         for(int j=0;j<list1.getlength();j++) {                             element sec = (element)list1.item(j);                             if(sec.hasattributes()) {                                 string attr = sec.getattribute("value");                                 system.out.println(attr);                                 accountnameslist.add(attr);                             }                         }                     }                  }              }             system.out.println("accountnameslist.size()"+accountnameslist.size());              return null;         } catch (exception e) {               e.printstacktrace();           }          return null;     } 

thanks, rajya


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -