13 Replies Latest reply on May 28, 2010 12:36 AM by EJP

    Parsing Data recieved from  Socket

    807580
      I am Recieveing Data from Socket

      BufferdReader bfReader = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));


      from sender side i am sending it using C# Application by Creating DataTable and Populating it(it has 6 columns) and then using its writexml function i write xml to stringwriter object and then converting it to string and sending on socket.

      How can i parse it here and get individual values of columns by name here for each row sent from there.

      Edited by: Naseerqau on May 27, 2010 5:26 AM
        • 1. Re: Parsing Data recieved from  Socket
          800457
          If you are sending XML then just pass it to a parser and extract what you need from the document. I would suggest you use something like JDOM to do the parsing as it will make life much easier.
          • 2. Re: Parsing Data recieved from  Socket
            807580
            Below is the String which i am recieveing from socket,

            <DocumentElement>
            <Alerts>
            <Alert_Code>1</Alert_Code>
            <Alert_Message>Message1</Alert_Message>
            <USER_IP>172.17.77.5</USER_IP>
            <USER_ID>sahibzadamuhammad</USER_ID>
            <Link />
            <Image_Path />
            <System_Name>sahibzadamuhammad</System_Name>
            </Alerts>
            <Alerts>
            <Alert_Code>2</Alert_Code>
            <Alert_Message>Message2</Alert_Message>
            <USER_IP>172.17.77.5</USER_IP>
            <USER_ID>sahibzadamuhammad</USER_ID>
            <Link />
            <Image_Path />
            <System_Name>sahibzadamuhammad</System_Name>
            </Alerts>
            </DocumentElement>


            Here what i am trying to do but itgives exception of malformbytes

            DocumentBuilderFactory docFactory1 = DocumentBuilderFactory.newInstance();
            DocumentBuilder Builder1 = docFactory1.newDocumentBuilder();
            Document doc1 = Builder1.parse(requestSocket.getInputStream(),"Alerts");
            • 3. Re: Parsing Data recieved from  Socket
              JoachimSauer
              You need to use a.) an XML parser and b.) some code that understands the concrete XML format that you get.

              If you want to feed the data to an XML parser for part a, then you must not create the reader yourself.XML format specifies how the encoding of the data is specified and if you produce the Reader yourself you assume some encoding (which could be wrong) and prevent the XML parser from finding the correct encoding.

              Simply pass the InputStream itself to the XML parser (possibly after wrapping it in a BufferedInputStream for performance reasons, if necessary).

              For the actual XML parsing you can use one of the built-in technologies that Java provides. JAXP, SAX, StAX, DOM and JAXB are some of the possible technologies you should look into. JDOM is a third-party library similar to DOM but with a much nicer interface.

              SAX/StAX/DOM/JDOM are mostly low-level tools, JAXB is a higher-level tool that allows mapping specific XML to Java objects. That might be the nicest approach if the XML format is well-defined.
              • 4. Re: Parsing Data recieved from  Socket
                807580
                Exception : Invalid byte 1 of 1-byte UTF-8 sequence
                • 5. Re: Parsing Data recieved from  Socket
                  JoachimSauer
                  Naseerqau wrote:
                  Exception : Invalid byte 1 of 1-byte UTF-8 sequence
                  Bad question: not providing context in the form of code/explanation/input/expected output.
                  • 6. Re: Parsing Data recieved from  Socket
                    807580
                    Below is the String which i am recieveing from socket,
                    That string seems okay.

                    However your code snippet shows (correctly) using an InputStream (byte-oriented) and not something character-oriented (string).

                    How do you obtain the cited string? Maybe something is lost on the way.
                    • 7. Re: Parsing Data recieved from  Socket
                      807580
                      Here is input which i am sending on socket

                      DataTable dtAlerts = new DataTable("Alerts");
                      dtAlerts.Columns.Add("Alert_Code");
                      dtAlerts.Columns.Add("Alert_Message");
                      dtAlerts.Columns.Add("USER_IP");
                      dtAlerts.Columns.Add("USER_ID");
                      dtAlerts.Columns.Add("Link");
                      dtAlerts.Columns.Add("Image_Path");
                      dtAlerts.Columns.Add("System_Name");

                      dtAlerts.Rows.Add(1,"Message1","127.0.0.1","mad","http://forums.sun.com","imgepath","made");

                      using (StringWriter sw = new StringWriter())
                      {
                      dtAlerts.WriteXml(sw);
                      Message = sw.ToString();
                      sw.Dispose();
                      }

                      BWrite(Message); // BWriter is a Binary Witer

                      ************
                      now i want to get these paramerte on other side and stoer each seperately in respective variables

                      code i am using is

                      Socket requestSocket = openSocket(address, port);

                      DocumentBuilderFactory docFactory1 = DocumentBuilderFactory.newInstance();
                      DocumentBuilder Builder1 = docFactory1.newDocumentBuilder();

                      One///
                      inStream = new BufferedInputStream(requestSocket.getInputStream());
                      Document doc1 = Builder1.parse(inStream);
                      Or //
                      Document doc1 = Builder1.parse(requestSocket.getInputStream());

                      but on both methods i am getting the exception i posted earlier

                      Now i am lost and dont know what to do
                      • 8. Re: Parsing Data recieved from  Socket
                        807580
                        Thats how got that string that i posted earlier (which i get from socket)

                        inStream = new BufferedInputStream(requestSocket.getInputStream());
                        int bytesRead = 0;
                        byte[] buffer = new byte[1024];

                        while ((bytesRead = inStream.read(buffer)) != -1)
                        {

                        String chunk = new String(buffer, 0, bytesRead);
                        }
                        • 9. Re: Parsing Data recieved from  Socket
                          807580
                          Can you tell which encoding is used here?
                          String chunk = new String(buffer, 0, bytesRead);
                          Your C# server might insert some [byte order mark|http://en.wikipedia.org/wiki/Byte_order_mark] at the beginning of the message which might make the Java side choke.
                          • 10. Re: Parsing Data recieved from  Socket
                            807580
                            uses default characterset of platform
                            • 11. Re: Parsing Data recieved from  Socket
                              807580
                              uses default characterset of platform
                              So can you tell which encoding is used?
                              The point is that it is usually not good to rely upon the platform default encoding.

                              My suspicion is that you do not reliably reproduce the obtained bytes on the Java side.
                              • 12. Re: Parsing Data recieved from  Socket
                                DrClap
                                BIJ001 wrote:
                                uses default characterset of platform
                                So can you tell which encoding is used?
                                The point is that it is usually not good to rely upon the platform default encoding.

                                My suspicion is that you do not reliably reproduce the obtained bytes on the Java side.
                                Note that the error message says that it's using UTF-8 and finding a problem with that. Presumably you aren't using UTF-8, but some other encoding which generates bytes which are (naturally) inconsistent with UTF-8.
                                • 13. Re: Parsing Data recieved from  Socket
                                  EJP
                                  inStream = new BufferedInputStream(requestSocket.getInputStream());
                                  Don't use that code. Use a BufferedReader and specify the charset.