Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

apache Xerces2 ;parse a datainputstream instead of a file

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
I am trying to parse a datainputstream of xml input. I cant find it anywhere in the API specs.

thanks

The incoming xml looks like this:

Code:
<?xml version="1.0" ?><message><body>Some text</body><username>james</username>

The code will look something like this:
Code:
DataInputStream dataInputStream = new DataInputStream (...);
String msg = dataInputStream.readUTF();
Parser myParser = new SAXParser();
....
 
Not sure if you mean you don't know what package DataInputStream is in, or you don't know how to parse a stream.

Considering Stefan did the first, I'll do the second :

Code:
DataInputStream dataInputStream = new DataInputStream (...);
org.xml.sax.InputSource is = new org.xml.sax.InputSource(dataInputStream);
Parser myParser = new SAXParser();
myParse.parse(is);

--------------------------------------------------
Free Database Connection Pooling Software
 
Sorry:

I am mistaken. What I am trying to do is take a DataInputStream fom a network socket. but is being read as a String: msg is a readUTF method.

The incoming datainputstream looks like:
Code:
<?xml version='1.0'?><message><body>ffff<body><userName>james
</userName></message>

The code:
Code:
i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
String msg = i.readUTF ();
SAXParser parser = new SAXParser();
parser.setContentHandler(this);
parser.parse(msg);

I get this error:

Code:
java.net.MalformedURLException: no protocol: <?xml version='1.0'?><message><body>ffff<body><userName>james
</userName></message>

thanks
 
Well, considering that all XML messages are ASCII, why read the stream as UTF ??

This will suffice :

Code:
StringBuffer xmlMessage = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
  xmlMessage.append(line);
}
br.close();
socket.close();

System.out.println(xmlMessage.toString());

--------------------------------------------------
Free Database Connection Pooling Software
 
Here is the resulting code. I do not get an error but I also do not get any output.

thanks

Code:
public class ChatHandler extends Thread {
  protected Socket s;
  protected DataInputStream i;
  protected DataOutputStream o;
  protected static Vector handlers = new Vector ();
  protected ChatXMLParser chatXMLParser;
  protected BufferedReader br;

  public ChatHandler (Socket s) throws IOException {
    this.s = s;
    //i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
	br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
  }

  public void run () {
    String name = s.getInetAddress ().toString ();
    try {
      broadcast (name + " has joined.");
      handlers.addElement (this);
      while (true) {
		StringBuffer xmlMessage = new StringBuffer();
		String line = "";
		System.out.println(br.readLine());
		while ((line = br.readLine()) != null) {
		  xmlMessage.append(line);
		}
		System.out.println(xmlMessage.toString());
      }
    } catch (IOException ex) {
      ex.printStackTrace ();
    } finally {
      handlers.removeElement (this);
      broadcast (name + " has left.");
      try {
        s.close ();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

  protected static void broadcast (String message) {
    synchronized (handlers) {
      Enumeration e = handlers.elements ();
      while (e.hasMoreElements ()) {
        ChatHandler c = (ChatHandler) e.nextElement ();
        try {
          synchronized (c.o) {
            c.o.writeUTF (message);
          }
          c.o.flush ();
        } catch (IOException ex) {
          c.stop ();
        }
      }
    }
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top