java - Access data in xml as string -


i receiving xml in string format. there library search elements in string?

 <version value="0"/>     <issuedate>2017-12-15</issuedate>     <locale>en_us</locale>     <recipientaddress>         <category>primary</category>         <subcategory>0</subcategory>         <name>vitsi</name>         <attention>stowell group llc.</attention>         <addressline1>511 6th st</addressline1>         <city>lake oswego</city>         <country>united states</country>         <presentationvalue>lake oswego or 97034-2903</presentationvalue>         <state>or</state>         <zipcode>97034</zipcode>         <zip4>2903</zip4>     </recipientaddress>     <recipientaddress>         <category>additional</category>         <subcategory>1</subcategory>         <name>vitsi</name>         <addressline1>po box 957</addressline1>         <city>lake oswego</city>         <country>united states</country>         <presentationvalue>lake oswego or 97034-0104</presentationvalue>         <state>or</state>         <zipcode>97034</zipcode>         <zip4>0104</zip4>     </recipientaddress>     <sendername>tmo</sendername>     <senderid>il</senderid>     <senderaddress>         <name>t-mobile</name>         <addressline1>po box 790047</addressline1>         <city>st. louis</city>         <presentationvalue>st. louis mo 63179-0047</presentationvalue>         <state>mo</state>         <zipcode>63179</zipcode> . . . . 

i want access element recipientaddress, list. there library that? please note receive string. invoice , there many process, performance important

you use xpath. java has inbuilt support xml querying without thirdparty library,

code piece be,

string xmlinputstr = "<your_xml_string_input>"  string xpathexpressionstr = "<xpath_expression_string>"  documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); document doc = builder.parse(xmlinputstr); xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); xpathexpression expr = xpath.compile(xpathexpressionstr); 

you can write own expression string querying. typical example

"/recipientaddress/category" 

evaluate xml against expression retrieve list of nodes.

nodelist nodes = (nodelist) expr.evaluate(doc, xpathconstants.nodeset); 

and iterate on nodes,

for (int = 0; < nodes.getlength(); i++) {    node nnode = nodes.item(i);    ... } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -