通过xpath的方式进行解析,一次只能解析某个字段的值,如下只能显示所有的C1,而要显示C2还要再设置为"//AAA/CCC/ROW/C2");
有没好的思路,将xpath设置为"//AAA/CCC/ROW",能实现将则自动将所有的ROW构造为记录对应为JAVABEAN,并将记录
放入arraylist,
这样访问 arraylist.get(i).get("C1"),就比较方便的获取各字段值了:import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class XMLReader {
public static void parseXmlToList(String xmlstring,String xpath){
SAXReader reader = new SAXReader();
Document document = null; try {
document = reader.read(new org.xml.sax.InputSource(
new java.io.StringReader(xmlstring)));
List list = document.selectNodes(xpath);
for (Iterator i = list.iterator(); i.hasNext();) {
Element e = (Element) i.next();
System.out.println("name=="+e.getName()+",value=="+e.getText());
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args){
parseXmlToList("<?xml version = '1.0' encoding = 'GBK'?>" +
"<AAA> " +
" <BBB>hello,bbb</BBB>" +
" <CCC>" +
" <ROW>" +
" <C1>111</C1>" +
" <C2>222</C2>" +
" </ROW>" +
" <ROW>" +
" <C1>333</C1>" +
" <C2>444</C2>" +
" </ROW>" +
" </CCC>" +
"</AAA>", "//AAA/CCC/ROW/C1");
}
}
有没好的思路,将xpath设置为"//AAA/CCC/ROW",能实现将则自动将所有的ROW构造为记录对应为JAVABEAN,并将记录
放入arraylist,
这样访问 arraylist.get(i).get("C1"),就比较方便的获取各字段值了:import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class XMLReader {
public static void parseXmlToList(String xmlstring,String xpath){
SAXReader reader = new SAXReader();
Document document = null; try {
document = reader.read(new org.xml.sax.InputSource(
new java.io.StringReader(xmlstring)));
List list = document.selectNodes(xpath);
for (Iterator i = list.iterator(); i.hasNext();) {
Element e = (Element) i.next();
System.out.println("name=="+e.getName()+",value=="+e.getText());
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args){
parseXmlToList("<?xml version = '1.0' encoding = 'GBK'?>" +
"<AAA> " +
" <BBB>hello,bbb</BBB>" +
" <CCC>" +
" <ROW>" +
" <C1>111</C1>" +
" <C2>222</C2>" +
" </ROW>" +
" <ROW>" +
" <C1>333</C1>" +
" <C2>444</C2>" +
" </ROW>" +
" </CCC>" +
"</AAA>", "//AAA/CCC/ROW/C1");
}
}
for (Iterator i = list.iterator(); i.hasNext();) {
Element e = (Element) i.next();
//绑定值到JAVABEAN对象ROW中
Row row = new Row();
row.setName(e.getName());
row.setValue(e.getText());
//将Row放入arrayList中
arrayList.add(row);
System.out.println("name=="+e.getName()+",value=="+e.getText());
}
<?xml version='1.0' encoding='utf-8'?>
<address-book>
<contact myType="individual">
<name>Zane Pasolini</name>
<address>999 W. Prince St.</address>
<city>New York</city>
<province>NY</province>
<postalcode>10013</postalcode>
<country>USA</country>
<telephone>1-212-345-6789</telephone>
</contact>
<contact myType="business">
<name>SAMOFIX d.o.o.</name>
<address>Ilica 47-2</address>
<city>Zagreb</city>
<province></province>
<postalcode>10000</postalcode>
<country from="cn">Croatia</country>
<telephone>385-1-123-4567</telephone>
</contact>
</address-book>
这是一份常用到的文件,现在我们需要将之映射到java bean,用Digester解析显得非常简单
public class AddressBookParser{
public void addContact(Contact contact)
{
System.out.println("TYPE: " + contact.getType());
System.out.println("NAME: " + contact.getName());
System.out.println(" ADDRESS: " + contact.getAddress());
System.out.println(" CITY: " + contact.getCity());
System.out.println(" PROVINCE: " + contact.getProvince());
System.out.println(" POSTALCODE: " + contact.getPostalcode());
System.out.println(" COUNTRY: " + contact.getCountry());
System.out.println(" COUNTRY-From: " + contact.getCountryFrom());
System.out.println(" TELEPHONE: " + contact.getTelephone());
} public static void main(String[] args) throws IOException, SAXException
{
// instantiate Digester and disable XML validation
Digester digester = new Digester();
digester.setValidating(false); // instantiate AddressBookParser class
digester.addObjectCreate("address-book", AddressBookParser.class );
// instantiate Contact class
digester.addObjectCreate("address-book/contact", Contact.class ); // set type property of Contact instance when 'type' attribute is found
//对有属性的值通过setProperties方法 digester.addSetProperties("address-book/contact", "myType", "type" ); // set different properties of Contact instance using specified methods
//addCallMethod与addBeanPropertySetter等价
// 参数 0代表一个参数,默认就是当前读的数据 digester.addCallMethod("address-book/contact/name", "setName", 0);
digester.addCallMethod("address-book/contact/address", "setAddress", 0);
digester.addCallMethod("address-book/contact/address", "setAddress",0);
digester.addCallMethod("address-book/contact/city", "setCity", 0);
digester.addCallMethod("address-book/contact/province", "setProvince", 0);
digester.addCallMethod("address-book/contact/postalcode", "setPostalcode", 0);
digester.addCallMethod("address-book/contact/country", "setCountry", 0); //增加country的属性 : from
digester.addSetProperties("address-book/contact/country","from","countryFrom");
digester.addCallMethod("address-book/contact/telephone", "setTelephone", 0); // call 'addContact' method when the next 'address-book/contact' pattern is seen
digester.addSetNext("address-book/contact", "addContact" ); // now that rules and actions are configured, start the parsing process
AddressBookParser abp = (AddressBookParser) digester.parse(new File("c:\\addressbook.xml"));
} /**
* JavaBean class that holds properties of each Contact entry.
* It is important that this class be public and static, in order for
* Digester to be able to instantiate it.
*/
public static class Contact
{
private String type;
private String name;
private String address;
private String city;
private String province;
private String postalcode;
private String country;
//增加一个country的属性: from
private String countryFrom;
private String telephone; public void setType(String newType)
{
type = newType;
}
public String getType()
{
return type;
} public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
} public void setAddress(String newAddress)
{
address = newAddress;
}
public String getAddress()
{
return address;
} public void setCity(String newCity)
{
city = newCity;
}
public String getCity()
{
return city;
} public void setProvince(String newProvince)
{
province = newProvince;
}
public String getProvince()
{
return province;
} public void setPostalcode(String newPostalcode)
{
postalcode = newPostalcode;
}
public String getPostalcode()
{
return postalcode;
} public void setCountry(String newCountry)
{
country = newCountry;
}
public String getCountry()
{
return country;
} public void setTelephone(String newTelephone)
{
telephone = newTelephone;
}
public String getTelephone()
{
return telephone;
} public String getCountryFrom() {
return countryFrom;
} public void setCountryFrom(String countryFrom) {
this.countryFrom = countryFrom;
}
}
}
在 AjaxChat 中的读取房间信息的方式显得更简洁
房间的xml配置文件如下:
<rooms>
<room id="1" name="General Topics" />
<room id="2" name="Programming" />
<room id="3" name="Movies" />
<room id="4" name="Music" />
<room id="5" name="Television" />
</rooms>
解析代码如下 :
public synchronized void init(InputStream isConfigFile) {
log.debug("init()");
if (isConfigFile != null) {
// Read in rooms config and create beans, hand off to DAO.
Digester digester = new Digester();
digester.setValidating(false);
digester.push(this);
digester.addObjectCreate("rooms/room",
"org.apache.struts.apps.ajaxchat.dto.RoomDTO");
//注意这里,如果xl的属性名称和bean的属性名称完全对应,则直接提供xml的位置即可
digester.addSetProperties("rooms/room");
digester.addSetNext("rooms/room", "addRoom");
try {
digester.parse(isConfigFile);
log.info("***** Rooms = " + rooms);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
}
} } // End init().