XML解析
操作节点属性时,注意将Node 转成 Element,ELEMENT中有更多操作属性的方法 遍历属性时,需要判断当前节点是否为元素节点,用 Node.ELEMENT_NODE == node 用来判断,其中node为当前操作的节点
try{
}catch(Exception e){ throw new RuntimeException(e);//异常转义 }
xml文档校验(javascript)
<script type="text/javascript"> //创建解析器对象 var xmldoc = new ActiveXObject(Microsoft.XMLDOM); //开启xml校验 xmldoc.validateOnParse="true"; //装载xml文档 xmldoc.load("example.xml"); //把报错信息及行号打印在页面上 document.write("reason"+xmldoc.parseError.reason+"<br/>"); document.write("line number"+xmldoc.parseError.line+"<br/>");
</script>
xmlSchema
ResourceBundle类
public abstract class ResourceBundle
资源包包含特定于语言环境的对象。当程序需要一个特定于语言环境的资源时(如 String),程序可以从适合当前用户语言环境的资源包中
装入它。以这种方式可以编写很大程度上独立于用户语言环境的程序代码,它将资源包中大部分(如果不是全部)特定于语言环境的信息隔
离开来。
这就使您所编写的程序可以:
轻松地本地化或翻译成不同的语言 一次处理多个语言环境 以后可以轻松地进行修改,支持更多的语言环境
ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale);
使用指定的基本名称、默认的语言环境和调用方的类加载器获取资源包。调用此方法等效于调用: getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()),
配置文件*.properties进行解耦
studentDao = com.itheima.dao.StudentDaoDom4jImpl
创建工厂类来获取studentDao对象
public class ObjectFactory { //把工厂类设计为单例模式 private static String daoImplClass; static{ //具体创建的student对象类从配置文件文件中进行读取 ResourceBundle rb = ResourceBundle.getBundle("daofg"); daoImplClass = rb.getString("studentDao"); } private static final ObjectFactory instance = new ObjectFactory(); private ObjectFactory(){ } public static ObjectFactory getInstance(){ return instance;
} public studentDao getStudentDao(){ try { //根据从配置文件中读取的类名,利用反射获取studentDao的具体的对象 return (studentDao) Class.forName(daoImplClass).newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } }
junit @BeforeClass //类创建之前 public static void Before(){ dao = new StudentDaoDom4jImpl(); } @AfterClass //类创建之后 public static void After(){ dao =null; }
Do4j格式化更新xml文档
public static void Write2Xml(Document document)throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint(); OutputStream out = new FileOutputStream("src/examl.xml"); XMLWriter writer = new XMLWriter(out,formate); writer.write(document); writer.close(); }
xpath (匹配路径获取对应的XML元素)
eg:获取所有的元素 String xpath = "//name"; List<Node> nodes = document.selectNodes(xpath);
eg:根据examid获取学生//注意单引号的使用 String xpath = "//student[@examid='"+examid+"']"; Node node = document.selectSingleNode(xpath);
eg:获取第一本书//注意xpath中角标从1开始 String path = "//书[1]"; Node node = document.selectSingleNode(xpath);
浙公网安备 33010602011771号