Java中如何读取xml文件?
您可以参考下
*
* To change this template, choose Tools | Templates
* and open the template in the editor。
*/
package xml;
import 。*;
import 。File;
import 。FileInputStream;
import 。IOException;
import java。 util。logging。Level;
import java。util。logging。Logger;
import m。*;
import rsers。*;
import ...全部
您可以参考下
*
* To change this template, choose Tools | Templates
* and open the template in the editor。
*/
package xml;
import 。*;
import 。File;
import 。FileInputStream;
import 。IOException;
import java。
util。logging。Level;
import java。util。logging。Logger;
import m。*;
import rsers。*;
import ansform。
*;
import m。*;
import ream。StreamResult;
import x。SAXException;
public class MyXMLReader {
public String libPath = null;
public String filePath;
public void setFilePath(String filePath) {
lePath = filePath;
}
public String getLibPath() {
return libPath;
}
//得到用户xml中各个节点的值
public void getPathValue(String xmlPath) throws ParserConfigurationException, SAXException, IOException {
File f = new File(xmlPath);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = wInstance();
DocumentBuilder builder = wDocumentBuilder();
Document doc = rse(f);
NodeList n2 = tDocumentElement()。
getElementsByTagName("context-param");
for (int i = 0; i < tLength(); i++) {
String name = tElementsByTagName("param-name")。
item(i)。getFirstChild()。getNodeValue();
String value = tElementsByTagName("param-value")。
item(i)。getFirstChild()。getNodeValue();
if (name。equals("libPath")) {
libPath = value;
}
}
}
//将要更新文件中的值替换掉
public void setPathValue() throws ParserConfigurationException, SAXException, IOException {
String xml="。
/hzims/Tomcat6/webapps/ROOT/WEB-INF/web。xml";
File f = new File(xml);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = wInstance();
DocumentBuilder builder = wDocumentBuilder();
Document doc = rse(f);
NodeList n2 = tDocumentElement()。
getElementsByTagName("context-param");
for (int i = 0; i < tLength(); i++) {
String name = tElementsByTagName("param-name")。
item(i)。getFirstChild()。getNodeValue();
String value = tElementsByTagName("param-value")。
item(i)。getFirstChild()。getNodeValue();
if (name。equals("libPath")) {
tElementsByTagName("param-value")。
item(i)。getFirstChild()。setNodeValue(libPath);
try {
reSetValue(doc,"。
/hzims/Tomcat6/webapps/ROOT/WEB-INF/web。xml" );
} catch (FileNotFoundException ex) {
tLogger( tName())。
log(Level。SEVERE, null, ex);
} catch (TransformerConfigurationException ex) {
tLogger( tName())。
log(Level。SEVERE, null, ex);
} catch (TransformerException ex) {
tLogger( tName())。
log(Level。
SEVERE, null, ex);
}
}
}
}
public void reSetValue(Document doc, String path) throws FileNotFoundException, IOException, TransformerConfigurationException,
TransformerException {
TransformerFactory tFactory = wInstance();
Transformer transformer = wTransformer();
//设置输出的encoding为改变gb2312
tOutputProperty("encoding", "gb2312");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
ansform(source, result);
}
}。收起