JDK6笔记(2)----操作XML文件

简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1529469 JDK6笔记(2)----操作XML文件一、XML文件如下:文件名为:de_pjxmb.
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1529469

JDK6笔记(2)----操作XML文件

一、XML文件如下:
文件名为:de_pjxmb.xml

<?xml version="1.0" encoding="gb2312"?>
<object title="价格明细表项" name="tbjgmx" pname="TBJGMX" log_table="system.LOG.0002" ts_column="lts">
<property title="ID" name="id" pname="ID" type="int"/>
<property title="时戳" name="lts" pname="LTS" type="int"/>
<property title="工作单ID" name="gzdid" pname="GZDID" type="int"/>
<property title="填报单位" name="tbdw" pname="TBDW" type="int" cc="2"/>
<property title="产品ID" name="cpid" pname="CPID" type="int"/>
<property title="价格类型" name="jglx" pname="JGLX" type="int" cc="3"/>
<property title="结算方式" name="jsfs" pname="JSFS" type="int" cc="17"/>
<property title="包装类型" name="bzlx" pname="BZLX" type="int" cc="6"/>
<property title="市场区域" name="scqy" pname="SCQY" type="int" cc="2" cl="1"/>
<property title="生效标志" name="sxbz" pname="SXBZ" type="int"/>
<property title="生效日期" name="sxrq" pname="SXRQ" type="date"/>
<property title="结束日期" name="jsrq" pname="JSRQ" type="date"/>
<property title="外键" name="fk" pname="FK" type="string" length="40"/>
<property title="外键新" name="fkn" pname="FKN" type="sting" length="40"/>
</object>


二、Java源文件如下:
package myfile;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;

import java.io.FileNotFoundException;
import java.io.FileReader;
public class XmlTest1 {
 public static void main(String[] args) throws XMLStreamException, FileNotFoundException{
  String path="D:/eclipse/Workspace/Test/src/myfile/de_pjxmb.xml";
  FileReader file=new FileReader(path);  
  XMLInputFactory factory=XMLInputFactory.newInstance();
  XMLStreamReader r=factory.createXMLStreamReader(file);
  try{
   int event=r.getEventType();
   while(true){
    switch(event){
    case XMLStreamConstants.START_DOCUMENT: System.out.println("Start Document.");
    break;
    case XMLStreamConstants.START_ELEMENT: System.out.println("Start Element:"+r.getName());
    for(int i=0,n=r.getAttributeCount();i<n;++i)
     System.out.println("Attribute: "+r.getAttributeName(i));
    break;
    case XMLStreamConstants.CHARACTERS:
     if(r.isWhiteSpace())
      break;
     System.out.println("Text: "+r.getText());
     break;
    case XMLStreamConstants.END_ELEMENT: System.out.println("End Element:"+r.getName());
    break;
    case XMLStreamConstants.END_DOCUMENT: System.out.println("End Document.");
    break;
    }
    if(!r.hasNext())
     break;
    event=r.next();
   }
  }
  finally{
   r.close();
  }
 } 
}

三、输出结果如下:

Start Document.
Start Element:object
Attribute: title
Attribute: name
Attribute: pname
Attribute: log_table
Attribute: ts_column
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
Attribute: cl
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: length
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: length
End Element:property
End Element:object
End Document.
 

目录
相关文章
|
20天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
21 0
|
1月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
33 0
|
1月前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
2月前
|
SQL
Mybatis.xml文件中大于小于等于
Mybatis.xml文件中大于小于等于
9 0
|
2月前
|
XML 关系型数据库 MySQL
【Mysql】有关数据库中一对多/一对一,多对一xml中文件映射问题
【Mysql】有关数据库中一对多/一对一,多对一xml中文件映射问题
19 0
|
2月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
12 0
|
2月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
11 0
|
26天前
|
XML 数据格式 Windows
如何从xml文件创建R语言数据框dataframe
如何从xml文件创建R语言数据框dataframe
|
2天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
10 1
|
2天前
|
Android开发
android string.xml文件中的整型和string型代替
android string.xml文件中的整型和string型代替