修改XML怎么在源XML中没有起作用那??
XML文件是:
<?xml version="1.0" encoding="gbk"?>
<values>
<value>zh</value>
</values>
java代码是这样的:
package com.xaut.wangwuyi.tts.util;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class ReadXmlResource
{
public String getI18Xml()
{
String values ="";
try
{
File f = new File("resource.xml");
//InputStream is = getClass().getClassLoader().getResourceAsStream("message_"+i18+".properties");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("values");
for (int i = 0; i < nl.getLength(); i++)
{
values = doc.getElementsByTagName("value").item(i).getFirstChild().getNodeValue();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return values;
}
/**
* 这种方式源文件没改动
* @param language
*/
public void setI18Xml2(String language)
{
try
{
File f = new File("resource.xml");
//InputStream is = getClass().getClassLoader().getResourceAsStream("message_"+i18+".properties");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("values");
for (int i = 0; i < nl.getLength(); i++)
{
doc.getElementsByTagName("value").item(i).getFirstChild().setNodeValue(language);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 这种方式源文件没改动
* @param language
*/
public void setI18NXml(String language)
{
try
{
File f = new File("resource.xml");
//InputStream is = getClass().getClassLoader().getResourceAsStream("message_"+i18+".properties");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
Element root = doc.getDocumentElement();
Element theBook=(Element) selectSingleNode("/values", root);
theBook.getElementsByTagName("value").item(0).setTextContent(language);