[jdom-interest] XMLOutputter to Sockets

Uli Waibel uwaibel at t-online.de
Tue Feb 26 13:26:45 PST 2002


Hi,

I try to get the following simple client/ server app running, but having
problems.
Any help is really apreciated

Thanks a lot

Uli Waibel
JAVA Developer, Germany

<<< CODE for Server >>>>>

public void server()
{
	try
	{
		ServerSocket srv = new ServerSocket(2222);

		SAXBuilder builder = new SAXBuilder(false);

		try
		{
			while( true )
			{
				System.out.println("Wait for Client");

				Socket in = srv.accept();

				System.out.println("Client connected");

				Document doc = builder.build(new
java.io.BufferedInputStream(in.getInputStream()));

				System.out.println( "Document created...");

				Element root = doc.getRootElement();
				org.jdom.Namespace ns = root.getNamespace();

				Element command = root.getChild("Action",ns).getChild("Command",ns);

				System.out.println( command.getName()+": "+command.getText() );

				List parms =
root.getChild("Action",ns).getChild("Parameter",ns).getChildren();

				List retParms  =
root.getChild("Action",ns).getChild("Return",ns).getChildren();

				Iterator it = parms.iterator();

				String user=null;

				while( it.hasNext() )
				{
					Element aParam = (Element)it.next();
					if( aParam.getName().compareTo("UserID")== 0 )
						user = aParam.getText();
					if( aParam.getName().compareTo("Passwd")== 0 )
					{
						if( aParam.getAttribute("encrypt").getBooleanValue() == true )
							System.out.println("Passwort decodieren");
					}

					//System.out.println( aParam.getName()+": " +aParam.getText());
				}

				it = retParms.iterator();
				while( it.hasNext() )
				{
					Element aParam = (Element)it.next();

					if( aParam.getName().compareTo("Returnmsg")== 0 )
					{
						aParam.setText("Benutzer " + user + " ist angemeldet");
					}
				}

				org.jdom.output.XMLOutputter printer = new
org.jdom.output.XMLOutputter();
				printer.output( doc, in.getOutputStream());
			}
		}
		catch( java.io.IOException e ){e.getMessage();}

	}
	catch( JDOMException e ){e.printStackTrace();}
	catch( java.io.IOException e ){e.printStackTrace();}
}


<<< Client Code >>>>
public static void main(java.lang.String[] args) {

	try
	{
		java.net.Socket tester = new java.net.Socket("localhost", 2222);

		SAXBuilder builder = new SAXBuilder(false);
		Document doc = builder.build(new File( args[0] ));

		System.out.println( "Document created...");

		// in reallity I will modifiy the elements of a document here
		// .....

		// and send to server
		XMLOutputter printer = new XMLOutputter();
		printer.output(doc, tester.getOutputStream());

		doc=builder.build(tester.getInputStream());
		printer.output(doc,System.out);

	}
	catch( java.net.UnknownHostException e ){e.printStackTrace();}
	catch( java.io.IOException e){e.printStackTrace();}
	catch( JDOMException e ){e.printStackTrace();}
}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: Ulrich Waibel.vcf
Type: text/x-vcard
Size: 519 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20020226/22c02fe6/UlrichWaibel.vcf


More information about the jdom-interest mailing list