How to copy the contents an incoming XML from an application and store it in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sujatha0105
    New Member
    • May 2010
    • 3

    How to copy the contents an incoming XML from an application and store it in a file

    I need to copy the contents of an incoming XML from an application into a file present at another location. This incoming XML is being using XMLStreamReader and is being stored in the HashMap table. The commands used are reader.getAttri butValue(), where reader is the object of XMLStreamReader .
    Note: Since the incoming XML is from an application, it doesn’t have a fixed location (JMETER/JBOSS is being used).
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    "This incoming XML is being using XMLStreamReader and is being stored in the HashMap table."
    Please clarify: Are you storing the xml into the file as it was, or are you restructuring the output in some different way?

    Trying to figure out what part you're having troubles with:
    1. Writing to a file?
    2. Converting the Hashmap to an output stream?
    3. XML output problems? (eg namespace)

    Comment

    • Sujatha0105
      New Member
      • May 2010
      • 3

      #3
      Originally posted by jkmyoung
      "This incoming XML is being using XMLStreamReader and is being stored in the HashMap table."
      Please clarify: Are you storing the xml into the file as it was, or are you restructuring the output in some different way?

      Trying to figure out what part you're having troubles with:
      1. Writing to a file?
      2. Converting the Hashmap to an output stream?
      3. XML output problems? (eg namespace)
      Since the XML is coming from another application, it is not stored anywhere, and what I am trying to do is write it on to a file and store it at a location.

      To answer to your question, I am facing trouble in writing the incoming XML to a file.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        What problem you are facing?

        Regards
        Dheeraj Joshi

        Comment

        • Sujatha0105
          New Member
          • May 2010
          • 3

          #5
          This is what i have tried..

          FileWriter F2 = new FileWriter("../inputlog/1.log");
          BufferedWriter D2 = new BufferedWriter( F2);
          while (reader.hasNext ())
          {
          if (log.isDebugEna bled())
          {
          log.debug("inpu t xml value:" + reader.getText( ));
          }
          String a1 = reader.getText( );
          D2.write(a1);
          // some other code is present here
          .
          .
          .
          reader.next();
          }
          F2.close();

          Can I know what’s wrong in the above code

          Note:
          1) reader is an object of XMLStreamReader
          2) Also the file "1.log" gets created but doesn’t have any Data
          3) The entire system is in UNIX

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            Is this the complete code?

            I can not see D2.close() being called. You need to close D2.

            Regards
            Dheeraj Joshi

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7

              getText() is not always valid depending on where you are in the document. This is not like a bufferredReader where you can just call next() or nextLine(). From the page:
              "The XMLStreamReader is designed to iterate over XML using next() and hasNext(). The data can be accessed using methods such as getEventType(), getNamespaceURI (), getLocalName() and getText();"

              Basically your output relies on the state you are in:
              Switch on next(){
              output current value in different ways, depending on your state.
              }

              Use a XMLStreamWriter to make it easier: Why rewrite code if you don't have to?

              Comment

              Working...