修改XML怎么在源XML中没有起作用那??
XML文件是:
java代码是这样的:
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);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Node selectSingleNode(String express, Object source){//查找节点,并返回第一个符合条件节点
Node result=null;
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
try {
result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return result;
}
public static void main(String arge[]) throws Exception
{
ReadXmlResource g = new ReadXmlResource();
System.out.println(123);
g.setI18NXml("en");
System.out.println("dsfa " + g.getI18Xml());
}
}