- ye_jinghua
- 等级: 初级会员
- 性别:
- 文章: 14
- 积分: 50
- 来自: 北京
|
在页面上选择多个用户ID用 XML+javascript 简单实现
首先生成用户信息的XML文件,生成代码如下:
java 代码
-
- import org.w3c.dom.*;
- import javax.xml.parsers.*;
- import javax.xml.transform.*;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import java.io.*;
- public class Test {
- private Document document;
- private String filename;
-
- public Test(String name) throws ParserConfigurationException{
- filename=name;
- DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
- DocumentBuilder builder=factory.newDocumentBuilder();
- document=builder.newDocument();
- }
- public void toWrite(Element root,String textuserName,String textcnName,String userId){
- Element User=document.createElement("User");
- root.appendChild(User);
- Element userid=document.createElement("userid");
- userid.appendChild(document.createTextNode(userId));
- User.appendChild(userid);
- Element userName=document.createElement("userName");
- userName.appendChild(document.createTextNode(textuserName));
- User.appendChild(userName);
- Element cnName=document.createElement("cnName");
- cnName.appendChild(document.createTextNode(textcnName));
- User.appendChild(cnName);
- }
- public void toSave(){
- try{
- TransformerFactory tf=TransformerFactory.newInstance();
- Transformer transformer=tf.newTransformer();
- DOMSource source=new DOMSource(document);
- transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
- transformer.setOutputProperty(OutputKeys.INDENT,"yes");
- PrintWriter pw=new PrintWriter(new FileOutputStream(filename));
- StreamResult result=new StreamResult(pw);
- transformer.transform(source,result);
- }
- catch(TransformerException mye){
- mye.printStackTrace();
- }
- catch(IOException exp){
- exp.printStackTrace();
- }
- }
- public static void main(String args[]){
- try{
- 在WEB中得到绝对路径
-
-
-
-
-
- Test myxml=new Test("E:\\users.xml");
- Element root=myxml.document.createElement("Users");
- myxml.document.appendChild(root);
- myxml.toWrite(root,"admin","管理员","001");
- myxml.toWrite(root,"yjh","叶先生","002");
- myxml.toWrite(root,"yjw","杨先生","003");
- myxml.toSave();
- System.out.print("Your writing is successful.");
- }
- catch(ParserConfigurationException exp){
- exp.printStackTrace();
- System.out.print("Your writing is failed.");
- }
- }
- }
HTML中引用的xml 代码
- <?xml version="1.0" encoding="GB2312"?>
- <Users>
- <User>
- <userid>647</userid>
- <userName>小李</userName>
- <cnName>小李</cnName>
- </User>
- <User>
- <userid>1</userid>
- <userName>admin</userName>
- <cnName>管理员</cnName>
- </User>
- <User>
- <userid>2</userid>
- <userName>qq</userName>
- <cnName>QQ</cnName>
- </User>
- <User>
- <userid>4</userid>
- <userName>sun</userName>
- <cnName>SUN</cnName>
- </User>
- <User>
- <userid>6</userid>
- <userName>small</userName>
- <cnName>SMALL</cnName>
- </User>
- <User>
- <userid>1745</userid>
- <userName>王处长</userName>
- <cnName>王天龙</cnName>
- </User>
- <User>
- <userid>648</userid>
- <userName>小王</userName>
- <cnName>小王</cnName>
- </User>
- </Users>
选择多用户的HTML页代码:
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|