Java读取xml文件

 

context.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE paths [

  <!ELEMENT paths (applicationcontext-path*,webapplicationcontext-path*)*>

  <!ELEMENT applicationcontext-path EMPTY>

  <!ATTLIST applicationcontext-path value CDATA #REQUIRED>

  <!ELEMENT webapplicationcontext-path EMPTY>

  <!ATTLIST webapplicationcontext-path value CDATA #REQUIRED>

]>

<paths>

<applicationcontext-path value="beans.xml" />

</paths>

 

 

package cn.yangbin.bjyq.utils;

 

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

 

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

 

public class SpringContextFactorySingleton {

private static ApplicationContext applicationContext;

static {

applicationContext = new ClassPathXmlApplicationContext(readApplicationContextPathXML());

}

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

public static String[] readApplicationContextPathXML() {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

List<String> contextsList = new ArrayList<String>();

try {

builder = factory.newDocumentBuilder();

InputStream path = SpringContextFactorySingleton.class.getClassLoader().getResourceAsStream("context-paths.xml");

Document document = builder.parse(path);

NodeList list = document.getDocumentElement().getElementsByTagName("applicationcontext-path");

if(list.getLength() > 0) {

Element element = null;                        

for(int i = 0; i < list.getLength(); i++) {

element = (Element) list.item(i);

if(!element.getAttribute("value").equals("")) {

contextsList.add(element.getAttribute("value"));

}

}

} else {

throw new Exception("路径文件不能为空");

}

} catch (Exception e) {

e.printStackTrace();

}

return (String[]) contextsList.toArray(new String[contextsList.size()]);

}

@Test public void ssss() {

System.out.println(getApplicationContext());

}

}

 

<!ELEMENT 元素名称类别>

<!ELEMENT 元素名称 (元素内容)>

<!ELEMENT 元素名称 EMPTY>

<!ATTLIST 元素名称属性名称 属性类型 默认值>

<!ENTITY 实体名称 "实体的值">

 

以下是属性类型的选项:

类型

描述

CDATA

值为字符数据 (character data)

(en1|en2|..)

此值是枚举列表中的一个值

ID

值为唯一的 id

IDREF

值为另外一个元素的 id

IDREFS

值为其他 id 的列表

NMTOKEN

值为合法的 XML 名称

NMTOKENS

值为合法的 XML 名称的列表

ENTITY

值是一个实体

ENTITIES

值是一个实体列表

NOTATION

此值是符号的名称

xml:

值是一个预定义的 XML 值

默认值参数可使用下列值:

解释

属性的默认值

#REQUIRED

属性值是必需的

#IMPLIED

属性不是必需的

#FIXED value

属性值是固定的

 

 

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

 

 

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

 

 

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

 

<?xml version="1.0"?>
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值