您的位置:首页 > 编程语言 > Java开发

java 解析XML生成bean

2011-04-28 11:41 351 查看
XML:

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>java</name>
<prise>99.0</prise>
<author>11</author>
<press>smg</press>
</book>
</books>


bean:

public class BookBean {

private String name;
private String prise;
private String author;
private String press;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrise() {
return prise;
}
public void setPrise(String prise) {
this.prise = prise;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}

}

解析类:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class XMLParseToBean implements ContentHandler {

private StringBuffer buffer = null;
private BookBean bean;

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
buffer.append(ch, start, length);
}

@Override
public void endDocument() throws SAXException {

}

@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if (localName.equalsIgnoreCase("name")) {
bean.setName(buffer.toString().trim());
} else if (localName.equalsIgnoreCase("prise")) {
bean.setName(buffer.toString().trim());
} else if (localName.equalsIgnoreCase("author")) {
bean.setName(buffer.toString().trim());
} else if (localName.equalsIgnoreCase("press")) {
bean.setName(buffer.toString().trim());
}
buffer.setLength(0);
}

@Override
public void endPrefixMapping(String prefix) throws SAXException {

}

@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {

}

@Override
public void processingInstruction(String target, String data)
throws SAXException {

}

@Override
public void setDocumentLocator(Locator locator) {

}

@Override
public void skippedEntity(String name) throws SAXException {

}

@Override
public void startDocument() throws SAXException {

}

@Override
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
buffer = new StringBuffer();
if (localName.equalsIgnoreCase("book")) {
bean = new BookBean();
}
}

@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {

}

public StringBuffer getBuffer() {
return buffer;
}

public void setBuffer(StringBuffer buffer) {
this.buffer = buffer;
}

public BookBean getBean() {
return bean;
}

public void setBean(BookBean bean) {
this.bean = bean;
}

public static void main(String[] args) {
String fileName = "test.xml";
String filePath = "D:";
File file = new File(filePath, fileName);
InputStream strm = null;

try {
strm = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

/**
* 如果xml是String形式的则先转换成流
*/
//strm = new ByteArrayInputStream(orgXml.getBytes());   //orgXml是要解析的String形式的XML

InputSource Source = new InputSource(strm);
XMLParseToBean hd = new XMLParseToBean();

try {
XMLReader parser = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");

parser.setErrorHandler(new XMLErrorHandler());
parser.setContentHandler(hd);

// 启用验证(暂时不校验)
parser.setFeature("http://xml.org/sax/features/validation", false);
parser
.setFeature(
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
false);
// 开始解析文档
parser.parse(Source);

//获取生成的bean
hd.getBean();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: