我是个.NET程序员,但近来需要用java来操作XML,因为软件的原因,不能使用dom4j,只能使用dom,我从网上找了些代码,大家看看为何我在每2秒钟运行时,发现3分钟后,系统就提示:
java.io.FileNotFoundException: time.xml (请求的操作无法在使用用户映射区域打开的文件上执行。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
我的代码如下,请大家着重看数据保存时的代码: public static String getsetTime(String xiangmu)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException,TransformerException,FileNotFoundException
{
String xfile="myxml/time.xml";
/*//只能类似于以下代码,其中传过来的参数 xiangmu就是下面的id值,time项可以有多行
<?xml version="1.0" encoding="UTF-8"?>
<root>
<time id="lvguan" now="2009-04-15 09:13:51"/>
</root>
*/
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String xintime=dateformat.format(new Date());
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db=factory.newDocumentBuilder();
Document xmldoc=db.parse(new File(xfile));
xpathFactory=XPathFactory.newInstance();
xpath=xpathFactory.newXPath();
Element theBook=(Element)xpath.evaluate("/root/time[@id='"+xiangmu+"']",xmldoc, XPathConstants.NODE);
String laotime=theBook.getAttribute("now");
theBook.setAttribute("now", xintime);
//以下保存修改的XML文件
TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(xmldoc);
StreamResult result=new StreamResult();
FileOutputStream fout=new FileOutputStream(xfile));
result.setOutputStream(fout);
fout.flush;
transformer.transform(source, result);
return laotime;
} 谢谢了!
java.io.FileNotFoundException: time.xml (请求的操作无法在使用用户映射区域打开的文件上执行。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
我的代码如下,请大家着重看数据保存时的代码: public static String getsetTime(String xiangmu)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException,TransformerException,FileNotFoundException
{
String xfile="myxml/time.xml";
/*//只能类似于以下代码,其中传过来的参数 xiangmu就是下面的id值,time项可以有多行
<?xml version="1.0" encoding="UTF-8"?>
<root>
<time id="lvguan" now="2009-04-15 09:13:51"/>
</root>
*/
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String xintime=dateformat.format(new Date());
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db=factory.newDocumentBuilder();
Document xmldoc=db.parse(new File(xfile));
xpathFactory=XPathFactory.newInstance();
xpath=xpathFactory.newXPath();
Element theBook=(Element)xpath.evaluate("/root/time[@id='"+xiangmu+"']",xmldoc, XPathConstants.NODE);
String laotime=theBook.getAttribute("now");
theBook.setAttribute("now", xintime);
//以下保存修改的XML文件
TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(xmldoc);
StreamResult result=new StreamResult();
FileOutputStream fout=new FileOutputStream(xfile));
result.setOutputStream(fout);
fout.flush;
transformer.transform(source, result);
return laotime;
} 谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货