javasocket发送xml_socket发送和接收XML并解析就是在 ⽤JAVA获取XML信息 第⼆步(SAX) 上⾯增加⼀点内容。⽤Socket发送和接收。
客户端⼝:XMLC.java
/*
* Created on 2004-11-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.*;
import java.io.*;
/**
* @author Shaird
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XMLClient {
static Socket sock;
public static void main(String[] args)throws Exception{
sock = new LocalHost(),8210);
PrintWriter write = new PrintWriter(new OutputStreamWriter (OutputStream()),true);
File file = new File("../shaird/l");  //⽤于发送的⽂件路径
BufferedReader read = new BufferedReader(new FileReader(file));
String str=null;
while((str = adLine()) != null)
write.println(str);
write.flush();
s.close();
}
}
服务端⼝: XMLS.java
/*
* Created on 2004-11-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.*;
l.sax.*;
l.sax.helpers.XMLReaderFactory;
/**
* @author Shaird
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XMLServer {
java xml是什么public static void main(String[] args) throws IOException,SAXException{
String URL="../shaird/l";
//通过指定解析器的名称来动态加载解析器
XMLReader reader = ateXMLReader("s.parsers.SAXParser"); //处理内容前要注册内容管理器
reader.setContentHandler(new XMLHandler());
ServerSocket server=new ServerSocket(8210);
Socket client=server.accept();
BufferedReader br=new BufferedReader(new InputStream())); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(URL,false)));
String str=null;
String xml=new String();
while((str = br.readLine()) != null){
xml+=str+"/n";
out.println(str);
}
out.flush();
System.out.println(xml);
InputSource inputSource= new InputSource(xml);
inputSource.setSystemId(URL);
reader.parse(inputSource);
client.close();
File f = new File(URL);
f.delete();
}
}
好了和之前的
⽤JAVA获取XML信息 第⼆步(SAX) ⾥⾯的内容XMLhandler.java联合⽤⽤看。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。