[jdom-interest] Using a reader to get XML problems

TassoA at trustmarque.ca TassoA at trustmarque.ca
Fri Jan 26 08:26:13 PST 2001


Hi I wrote a small client server application.

The client application send XML data using a buffered writter.
And the server application receives the data using a bufferedreader.

But some how I can get it to parse the data I get exceptions.

<?xml version='1.0'?>

<Merchant>
<ID>100000</ID>
<Username>uk</Username>
<Password>uktest2</Password>
</Merchant>

org.jdom.JDOMException: The root element is required in a well-formed
document.: Error on line 1: The root element is required in a well-formed
document. 	
	at org.jdom.input.SAXBuilder.build(SAXBuilder.java:315)
	at org.jdom.input.SAXBuilder.build(SAXBuilder.java:408)
	at jdomtest.main(jdomtest.java:65)
Root cause: org.jdom.JDOMException: Error on line 1: The root element is
required in a well-formed document.
	at org.jdom.input.SAXBuilder.build(SAXBuilder.java:316)
	at org.jdom.input.SAXBuilder.build(SAXBuilder.java:408)
	at jdomtest.main(jdomtest.java:65)

Server app code:

import java.io.File;
import java.io.*;

import org.jdom.Document;
import org.jdom.Namespace;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.*;
import java.net.*;

public class jdomtest {

  public jdomtest() {
  }
  public static void main(String[] args) {

    if(args.length != 1)
    {
      System.out.println("Uasage: jdomtest [filename]");
      return;
    }

    ServerSocket ssocket;
    Socket socket;
    BufferedReader in = null;
    String test = null;

    try
    {
      ssocket = new ServerSocket(5555);
      socket = ssocket.accept();
      in  = new BufferedReader(new
InputStreamReader(socket.getInputStream()));


    char []buffer = new char[4096];

      in.read(buffer);

      test = new String(buffer);

      //in.close();

      System.out.println(test);
    }
    catch(IOException e)
    {}


    try
    {

      SAXBuilder builder = new SAXBuilder(false);
      //Document doc = builder.build(new File(args[0]));
      Document doc = builder.build(in);
      System.out.println("SAX: Document successfully read");

      Element root = doc.getRootElement();

      String version = root.getAttribute("Version").getValue();

      System.out.println(version);

      String merchantID = new String();

 
System.out.println(root.getChild("Merchant").getChild("ID").getText());


      System.out.println(merchantID);
    }
    catch(JDOMException e)
    {
      e.printStackTrace();
    }

  }
}

Cleint app code:

import java.io.*;
import java.net.*;

public class CLient {

  public CLient() {
  }
  public static void main(String[] args) {
    CLient CLient1 = new CLient();

    try
    {
      Socket socket = new Socket("10.0.1.71", 5555);

      //PrintStream out = new PrintStream(socket.getOutputStream());

      BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));

      out.write("<?xml
version='1.0'?>\r\n\r\n<Merchant>\r\n<ID>100000</ID>\r\n<Username>uk</Userna
me>\r\n<Password>uktest2</Password>\r\n</Merchant>\n\r");
      out.flush();
    }
    catch(IOException e)
    {}
  }
}



More information about the jdom-interest mailing list