当前位置: 技术问答>java相关
请教关于JAVA编写DOM的初级问题,望高手提鞋!!!
来源: 互联网 发布时间:2015-05-30
本文导语: 您知道吗,您随意的那么指点我一小下,就胜我读十天书!先谢了!!! 是不是在写DOM的时候要先读入XML文档呢?读入的程序该怎么写? 我写了一个开头就写不下去了,请大家帮忙了! public static Document reaDcument(String filenam...
您知道吗,您随意的那么指点我一小下,就胜我读十天书!先谢了!!!
是不是在写DOM的时候要先读入XML文档呢?读入的程序该怎么写?
我写了一个开头就写不下去了,请大家帮忙了!
public static Document reaDcument(String filename)throws IOException,SAXEception
我看了一个例子,可它说的我都看不懂.谁能帮帮我,讲讲思想也可以啊!写DOM有什么步骤和要求以及经验?谢谢!
还有能不能给些自学能用得上的网站?
是不是在写DOM的时候要先读入XML文档呢?读入的程序该怎么写?
我写了一个开头就写不下去了,请大家帮忙了!
public static Document reaDcument(String filename)throws IOException,SAXEception
我看了一个例子,可它说的我都看不懂.谁能帮帮我,讲讲思想也可以啊!写DOM有什么步骤和要求以及经验?谢谢!
还有能不能给些自学能用得上的网站?
|
//menu.xml
会议短信
1000
sendnotice.jsp
//xmlwriter
//读写menu.xml
package xmlpro;
/**
* Title:
* Description:modify xml file
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
/**notice xmlwriter uses j2ee.jar*/
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import java.util.*;
import java.io.*;
public class xmlwriter {
public static void main(String args[]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("menu.xml");
doc.normalize();
//---取得变量----
String name="手机银行";
String id="1009";
String url="mb.jsp";
//-------------
Text textseg;
Element businessNode=doc.createElement("business");
Element nameNode=doc.createElement("name");
textseg=doc.createTextNode(name);
nameNode.appendChild(textseg);
businessNode.appendChild(nameNode);
Element idNode=doc.createElement("id");
textseg=doc.createTextNode(id);
idNode.appendChild(textseg);
businessNode.appendChild(idNode);
Element urlNode=doc.createElement("url");
textseg=doc.createTextNode(url);
urlNode.appendChild(textseg);
businessNode.appendChild(urlNode);
doc.getDocumentElement().appendChild(businessNode);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/**Use OutputKeys*/
Properties properties=transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING,"GB2312" );
properties.setProperty(OutputKeys.VERSION,"1.0");
transformer.setOutputProperties(properties);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("menu.xml"));
transformer.transform(source, result);
}catch(Exception e){
e.printStackTrace();
}
}
}
会议短信
1000
sendnotice.jsp
//xmlwriter
//读写menu.xml
package xmlpro;
/**
* Title:
* Description:modify xml file
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
/**notice xmlwriter uses j2ee.jar*/
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import java.util.*;
import java.io.*;
public class xmlwriter {
public static void main(String args[]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("menu.xml");
doc.normalize();
//---取得变量----
String name="手机银行";
String id="1009";
String url="mb.jsp";
//-------------
Text textseg;
Element businessNode=doc.createElement("business");
Element nameNode=doc.createElement("name");
textseg=doc.createTextNode(name);
nameNode.appendChild(textseg);
businessNode.appendChild(nameNode);
Element idNode=doc.createElement("id");
textseg=doc.createTextNode(id);
idNode.appendChild(textseg);
businessNode.appendChild(idNode);
Element urlNode=doc.createElement("url");
textseg=doc.createTextNode(url);
urlNode.appendChild(textseg);
businessNode.appendChild(urlNode);
doc.getDocumentElement().appendChild(businessNode);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/**Use OutputKeys*/
Properties properties=transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING,"GB2312" );
properties.setProperty(OutputKeys.VERSION,"1.0");
transformer.setOutputProperties(properties);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("menu.xml"));
transformer.transform(source, result);
}catch(Exception e){
e.printStackTrace();
}
}
}
|
让高手提鞋??
不会吧?
不会吧?