JDK 解析xml文件
来源:互联网 发布:软件测试简答题 编辑:程序博客网 时间:2024/05/22 12:18
在对xml的实际的应用中、有时候为了方便会使用java jdk默认的解析xml的方式,而不去使用插件来操作。废话不多说了,这个应用只适合初学者,高手就不用看了... ...
文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<GDF-featureClass>
<featureClass>
<name>Substation_Boundary</name>
<export-name>SubstationRegion</export-name>
<props>
<prop propname="esriname" FeatureFieldName="SUB" />
</props>
</featureClass>
</GDF-featureClass>
</config>
操作方式如下:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("GDF_PROP.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(input);
Element root = document.getDocumentElement();
NodeList list = root.getElementsByTagName("GDF-featureClass");
Element element = (Element) list.item(0);
NodeList titleList = element.getElementsByTagName("featureClass");
List<ConfileInfo> confList = new ArrayList<ConfileInfo>();
ConfileInfo fileInfo=null;
int count = titleList.getLength();
for (int i = 0; i < count; i++) {
fileInfo = new ConfileInfo();
Element featureClass = (Element) titleList.item(i);
String featureClassName = featureClass.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
String GDF_Defination_name = featureClass.getElementsByTagName("export-name").item(0).getFirstChild().getNodeValue();
//props
NodeList props = featureClass.getElementsByTagName("props");
String esriname =null;
String SUB =null;
for (int j = 0; j < props.getLength(); j++) {
Element propert = (Element) props.item(j);
esriname =propert.getElementsByTagName("prop").item(0).getAttributes().item(1).getFirstChild().getNodeValue();
SUB = propert.getElementsByTagName("prop").item(0).getAttributes().item(0).getLastChild().getNodeValue();
fileInfo.getProps().put(esriname, SUB);
}
fileInfo.setFeatureClassname(featureClassName);
fileInfo.setGDF_Defination_name(GDF_Defination_name);
//add to confList
confList.add(fileInfo);
}
还有是String型的xml文件的格式,这个的处理就会好很多,如下:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(loc.getBytes()));
if (doc.getElementsByTagName("coordinates").item(0).getFirstChild()!=null) {
String result = doc.getElementsByTagName("coordinates").item(0).getFirstChild().getNodeValue();
}
如此就可以获得coordinates 节点下的xml文件内容。
XML的解析方式有很多种,但是jdk 自带的这种是比较效率低的,但是没有像其他的那样,需要加入jar包来调用。
根据使用项目的应用来选择使用哪种方式去使用。
- JDK 解析xml文件
- 利用Java jdk(dom)解析和写入XML文件
- jdk读取xml文件
- 用jdk自带的xml解析器创建xml文件
- 利用jdk对xsd文件操作生成java实体并解析生成的xml
- 用JDK自带的包来解析XML文件(DOM+xpath)
- 【Java解析XML】【二】JDK中的XML解析操作
- XML----SAX解析XML文件
- XML---DOM4J解析XML文件
- XML DOM解析XML文件
- XML SAX解析xml文件
- XML jDOM解析XML文件
- XML DOM4J解析XML文件
- 解析XML文件___SAX解析
- java 解析 xml文件
- XML文件解析
- XML文件解析
- JstL解析XML文件
- 批处理迭代目录
- task相关函数的功能
- BuildPipeline.BuildPlayer 编译播放器
- linux内核编译过程及配置说明解释 (4)--processor type 与power management,ACPI options
- CSS兼容大全
- JDK 解析xml文件
- Android 对话框(Dialog)大全 建立你自己的对话框
- C++ AMP: C++ AMP Articles in MSDN Magazine April issue
- android 自适应 多屏幕支持
- RHEL6通过安装光盘或ISO文件配置yum安装X11 server
- 倒计时100天
- mongodb c++ 接口说明
- 在MAC下如何显示隐藏文件夹
- MongoDB基本命令