一、举例读取的文件为:test.xml
<?xml version="1.0" encoding="UTF-8"?> <IMIXML>
<SecDef Desc="国债" CentraQuoteBondIndic="Y">
<Instrmt ID="CFT" Sym="现券买卖"/>
</SecDef>
<SecDef Desc="债券" CentraQuoteBondIndic="X">
<Instrmt ID="CCC" Sym="是否"/>
</SecDef>
</IMIXML>
二 项目结构:
三 代码:
package parseXml; import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.HashMap; public class Test extends DefaultHandler {
public static SAXParserFactory parserFactory;
public static SAXParser parser;
public static HashMap<String, SecDef> map = new HashMap<String, SecDef>();
String xBondTransactableBondInformation_Id; // 债券代码
SecDef secdef;
Instrmt instrmt; @Override
public void startDocument() { } // 读取XML的<后开始执行
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) {
if ("SecDef".equals(qName)) {
secdef = new SecDef();
instrmt = new Instrmt();
secdef.setCentraQuoteBondIndic(attributes
.getValue("CentraQuoteBondIndic"));
secdef.setDesc(attributes.getValue("Desc"));
secdef.setInstrmt(instrmt);
}
if ("Instrmt".equals(qName)) {
xBondTransactableBondInformation_Id = attributes.getValue("ID");
instrmt.setId(attributes.getValue("ID"));
instrmt.setSym(attributes.getValue("Sym"));
} } @Override
public void characters(char[] ch, int start, int length) {
} // 读取XML的</后开始执行
@Override
public void endElement(String uri, String localName, String qName) {
map.put(
xBondTransactableBondInformation_Id, secdef);
} @Override
public void endDocument() { } /**
* 解析方法
*
* @return map
*/
public static HashMap<String, SecDef> loadXML(String filePath) {
try {
parserFactory = SAXParserFactory.newInstance();
parser = parserFactory.newSAXParser();
Test reader = new Test();
parser.parse(new InputSource(filePath), reader);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return map;
} public static HashMap<String, SecDef> resultMap; public static void main(String[] args) {
resultMap = loadXML("src/input/xml/test.xml"); for (String key : resultMap.keySet()) {
System.out.print("key=" + key + "\t");
System.out.println("value="
+ resultMap.get(key));
}
} }
Test.java
package parseXml; public class SecDef {
private String desc;//
private String CentraQuoteBondIndic;
private Instrmt instrmt; public SecDef() { } public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public String getCentraQuoteBondIndic() {
return CentraQuoteBondIndic;
} public void setCentraQuoteBondIndic(String centraQuoteBondIndic) {
CentraQuoteBondIndic = centraQuoteBondIndic;
} public Instrmt getInstrmt() {
return instrmt;
} public void setInstrmt(Instrmt instrmt) {
this.instrmt = instrmt;
} @Override
public String toString() {
return "SecDef{" +
"desc='" + desc + '\'' +
", CentraQuoteBondIndic='" + CentraQuoteBondIndic + '\'' +
", instrmt=" + instrmt +
'}';
}
}
SecDef.java
package parseXml; public class Instrmt {
private String id;
private String sym; public Instrmt() { } public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getSym() {
return sym;
} public void setSym(String sym) {
this.sym = sym;
} @Override
public String toString() {
return "Instrmt{" +
"id='" + id + '\'' +
", sym='" + sym + '\'' +
'}';
}
}
Instrmt.java
四 结果: