能不能像操作数据库那样读写xml文件?
解决方案 »
- 4千万行数据 随机读20000行
- 厉害
- 二分插入算法,请帮我看看我的代码错在那里?我已经走进死胡同了,脑子糊涂了.好几天想不通!
- 请问使用JDOM生成JTree该怎么配置classpath?(java.lang.NoClassDefFoundError: org/jdom/Document)
- Java中的并发性能问题 -- 请教了解Java运行机制的高手!
- 用JAVA开发系统用什么编译环境最好?
- 怎样实现在服务器端的多个文件压缩打包(包括子目录)?在线等,得到答案立即结贴
- 关于BigDecimal
- 关于线程
- 大家探讨一下JAVA打印!!!!!!!!!来者有分!
- ItemListener 没反应????
- 关于这个程序,大家来看看
要用到DocumentBuilderFactory,DocumentBuilder这些
..............import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;........... DomainModel domainModel = (DomainModel) request.getSession().getAttribute("TABLE");
InputSource is = new InputSource("d:\\huaxia\\huaxiaweb\\src\\main\\resources\\myapp.struts.xml");
is.setEncoding("UTF-8");
SAXBuilder sb = new SAXBuilder();
Document structsXML;
try {
structsXML = sb.build(is);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
logger.info(e1.getMessage());
return;
} catch (IOException e1) {
// TODO Auto-generated catch block
logger.info(e1.getMessage());
return;
} // StructsXML文件的根元素
Element root = structsXML.getRootElement(); Element actionMapping = root.getChild("action-mappings");
boolean haveMainAction = false;
boolean haveUpdateAction = false;
boolean haveCreateAction = false;
java.util.List<Element> elements = actionMapping.getChildren();
for(Element el: elements){
if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "MainAction"))
&& el.getAttributeValue("parameter").equals("method")
&& el.getAttributeValue("name").equals((domainModel.getTableNameInJavaFirstLow() + "Form"))){
logger.info(domainModel.getTableNameInJava() +"MainAction has already existed.");
haveMainAction = true;
}
if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "CreateAction"))
&& el.getAttributeValue("input").equals( ("/" + domainModel.getTableNameInJava() + "Create.jsp"))){
logger.info(domainModel.getTableNameInJava() + "CreateAction has already existed.");
haveCreateAction = true;
}
if( el.getAttributeValue("path").equals( ("/" + domainModel.getTableNameInJava() + "UpdateAction"))
&& el.getAttributeValue("input").equals( ("/" + domainModel.getTableNameInJava() + "Update.jsp"))){
logger.info(domainModel.getTableNameInJava() + "UpdateAction has already existed.");
haveUpdateAction = true;
}
}
InputSource is = new InputSource("d:\\huaxia\\huaxiaweb\\src\\main\\resources\\myapp.struts.xml");
把一个xml文件读入InputSource类 这个类是jdom的一个部分。
is.setEncoding("UTF-8");
SAXBuilder sb = new SAXBuilder();
Document structsXML;
try {
structsXML = sb.build(is);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
logger.info(e1.getMessage());
return;
} catch (IOException e1) {
// TODO Auto-generated catch block
logger.info(e1.getMessage());
return;
}
把这个文件和一个Document类关联起来得到这个Document的根元素
Element root = structsXML.getRootElement();
根元素的所有二级元素, 放在list中
java.util.List <Element> elements = actionMapping.getChildren(); 然后通过element.setAttributes等等方法来修改,
最后记得把这个document重新写入io流放入xml文件即可,
上google搜索jdom 有文档下载 英文
f = new File(SavingFilePath,SavingFileName);//你的xml的路径名和文件名
DocumentBuilderFactory factory=DocumentBuilderfactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
String str = doc.getElementsByTagName(属性).item(num).getFirstChild().getNodeValue();
//num=那个属性排在在第几就是几.
这样就可以读到xml的文件了...你就可以操作XML文件..
还是用第三方的包进行操作方便呢?如dom4j ,JDOM 等
小量的存储是可以的。
最好的用途是在通信,数据交互上。xpath查询语言可以很方便的查询xml,但是由于xml和关系数据库有明显区别,不要期望xpath能和t-sql一样
import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
public class WriteXML{
public void BuildXML() throws Exception {
Element root,student,number,name,age,firstname,lastname;
root = new Element("student-info"); //生成根元素:student-info
student = new Element("student"); //生成元素:student(number,name,age)
number = new Element("number");
name = new Element("name");
age = new Element("age");
firstname = new Element("firstname");
lastname = new Element("lastname");
Document doc = new Document(root); //将根元素植入文档doc中
root.setText("root_name");
number.setText("001");
firstname.setText("yu");
lastname.setText("haiming");
age.setText("24");
student.addContent(number);
student.addContent(name);
student.addContent(age);
name.addContent(firstname);
name.addContent(lastname);
root.addContent(student);
root.setAttribute("width", "100%");
root.setAttribute("height", "860px");
root.setAttribute("bgcolor", "red");
root.setAttribute("name", "student");
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格
XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));
}
public static void main(String[] args) throws Exception {
WriteXML w = new WriteXML();
System.out.println("Now we build an XML document .....");
w.BuildXML();
System.out.println("finished!");
}
}