5 Replies Latest reply on Jul 27, 2010 7:59 AM by Manik Surtani

    Can we able to store a xml file as a value in Infinispan cache.

    Savin Seven Newbie

      Dear All,

       

      Can we able to store a xml file as a value in Infinispan cache. If so please provide me a sample code snippet do achieve this. My advance thanks for the same.

       

      Thanks & Regards,

      Savin

        • 1. Re: Can we able to store a xml file as a value in Infinispan cache.
          Manik Surtani Master

          Of course you can.  Details would depend on how you want to store it though - as raw bytes or as a parsed XML DOM tree, or some other representation.

          • 2. Re: Can we able to store a xml file as a value in Infinispan cache.
            Savin Seven Newbie

            I want to store it as raw bytes and as well as a parsed XML. Kindly provide me the details, how to store these representations.

            • 3. Re: Can we able to store a xml file as a value in Infinispan cache.
              Manik Surtani Master

              You'll want to look at the documentation on your XML parser then. 

              • 4. Re: Can we able to store a xml file as a value in Infinispan cache.
                Savin Seven Newbie

                Dear Manik,

                 

                I have a set of questions to you.

                 

                QUESTION 1:

                I am having a plain Xml file. As per my understanding, If I have to store this file in Cache then I have to convert this file to String and then put it in cache as below code.

                 

                public class OurXmlTest {

                /**
                  * @param args
                  */
                public static void main(String[] args) {
                  String filepath = "D:\\Response.xml";
                  Configuration c = new Configuration(); 
                  c.setExpirationLifespan(60000);  
                  EmbeddedCacheManager manager = new DefaultCacheManager();
                  manager.defineConfiguration("demoConfig", c);  
                  Cache<String, String> cache = manager.getCache("demoConfig"); 
                  cache.put("test", convertXmlToString(filepath));
                  String test = (String) cache.get("test");  
                  System.out.println("XML String is : "+test);
                }

                public static String convertXmlToString(String filepath) {
                  StringBuffer output = new StringBuffer();
                 
                  try {
                   BufferedReader in = new BufferedReader(new FileReader(filepath));  
                   String st;
                   while ((st=in.readLine()) != null) {
                   output.append(st);
                   }
                   System.out.println(output.toString());
                   in.close();
                   }
                  catch (Exception fx) {
                   System.out.println("Exception " + fx.toString());
                  }
                  return output.toString();
                 
                }

                }

                 


                What I wanted to know is ? Is there any other better approach is there to store a xml file in cache rather than the above code. If so kindly let me know.

                 

                QUESTION 2:

                If I store the XML in Cache. Will I able to query the XML using Hibernate Search or Lucence? or I can only able to query Java objects using Hibernate Search and Lucene. Please clarify.

                 

                QUESTION 3:

                If I want to store it as a parsed XML DOM tree. How I can do that. Please provide me the sample code snippet for the same.

                 

                Hope I am loading you with questions Kindly help me out in this.

                • 5. Re: Can we able to store a xml file as a value in Infinispan cache.
                  Manik Surtani Master

                  If I store the XML in Cache. Will I able to query the XML using Hibernate Search or Lucence? or I can only able to query Java objects using Hibernate Search and Lucene. Please clarify.

                  If you store the XML as a String, then yes, you will be able to run a full-text search on the contents of the String.  If it is some other object type, as long as the attributes are properly annotated, you will be able to index and search for them.  See Querying Infinispan

                   

                  Hope I am loading you with questions Kindly help me out in this.

                  Yes, you are loading me with questions.  Dude you're asking me about how best to parse XML.  There are loads of techniques out there, well outside of the scope of Infinispan.  I recommend reading up on things like SAX parsers, DOM trees, maybe JAXB and perhaps pick up a good book or two on Java and XML.