如:
<servlet>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:org/config/applicationContext-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
路径就是包名.some.xml
仅供参考
xml解析还是用dom4j方便,import java.util.List
import org.dom4j.Document
import org.dom4j.DocumentException
import org.dom4j.Element
import org.dom4j.io.SAXReader
public class XMLPaser {
public static void main(String[] args) {
paserXML()
}
public static void paserXML(){
SAXReader reader = new SAXReader()
try {
// 读取XML文件
Document doc = reader.read("NewFile.xml")
Element root = doc.getRootElement()
System.out.println(root.getName())
List<Element> param = root.elements()
for (Element element : param) {
if(element.attributeValue("name").equals("a")){
System.out.println(element.getText())
}
}
} catch (DocumentException e) {
e.printStackTrace()
}
}
}
可以参考 :package com.zuxia.dom4j
import java.io.File
import java.io.FileOutputStream
import java.util.Iterator
import java.util.List
import java.util.Scanner
import org.dom4j.Document
import org.dom4j.Element
import org.dom4j.io.OutputFormat
import org.dom4j.io.SAXReader
import org.dom4j.io.XMLWriter
/**
*
* 使用dom4j解析xml
*
* 1. 创建解析器
*
* 2. 创建文档对象Document
*
* 3. 获取根节点
*
*/
public class Dom4jParseXML {
public static void main(String[] args) {
//1. 创建解析器
SAXReader saxreader = new SAXReader()
Document doc = null
try {
//2. 创建文档对象Document
doc = saxreader.read(new File("src/studentinfo.xml"))
} catch (Exception e) {
System.out.println("读取xml文件异常!")
}
//3. 获取根节点
Element root = doc.getRootElement()
//4. 获取元素
Iterator<Element>iter = root.elementIterator()
while(iter.hasNext()){
Element student = iter.next()
System.out.println("学号:"+student.attributeValue("stuno")+"\t姓名:"+student.elementText("name"))
}
//提示用户添加新的数据
Scanner sc = new Scanner(System.in)
System.out.println("请输入学号:")
String stuno = sc.nextLine()
System.out.println("请输入姓名:")
String name = sc.nextLine()
System.out.println("请输入年龄:")
String age = sc.nextLine()
//将数据添加在Document中
Element student = root.addElement("student")
student.addAttribute("stuno", stuno)
student.addElement("name").addText(name)
student.addElement("age").addText(age)
//3. 设置格式
OutputFormat format = OutputFormat.createCompactFormat()
format.setIndentSize(4)
format.setNewlines(true)
try {
//4. 保存xml文件
XMLWriter out = new XMLWriter(new FileOutputStream("src/studentinfo.xml"),format)
out.write(doc)
System.out.println("ok!!!")
} catch (Exception e) {
System.out.println("失败!")
}
System.out.println("完成了!")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)