public class Man {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.util.ArrayList;
import java.util.List;
public class Mans {
List<Man> mans = new ArrayList<Man>();
public void addMan(Man man) {
mans.add(man);
}
public List<Man> getMans() {
return mans;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Man man : mans) {
sb.append("man name:").append(man.getName()).append("age:").append(
man.getAge()).append(" | ");
}
return sb.toString();
}
}
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.digester.AbstractObjectCreationFactory;
import org.apache.commons.digester.Digester;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class DigesterTest {
List<Mans> manList;
public void xmlParse() throws IOException, SAXException {
Digester digester = new Digester();
manList = new ArrayList<Mans>();
digester.addFactoryCreate("mans", new AbstractObjectCreationFactory() {
@Override
public Object createObject(Attributes attributes) throws Exception {
// System.out.println(attributes.getValue("test"));
Mans mansInner = new Mans();
System.out.println(mansInner.toString());
manList.add(mansInner);
return mansInner;
}
});
digester.addFactoryCreate("mans/man",
new AbstractObjectCreationFactory() {
@Override
public Object createObject(Attributes attributes)
throws Exception {
int length = attributes.getLength();
int i = 0;
while (i < length) {
// System.out.println(attributes.getQName(i));
i++;
}
Man man = new Man();
man
.setAge(Integer.parseInt(attributes
.getValue("age")));
man.setName(attributes.getValue("name"));
return man;
}
});
digester.addSetNext("mans/man", "addMan");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL urls = classLoader.getResource("test.xml");
System.out.println(urls.toExternalForm());
urls = this.getClass().getClassLoader().getResource("test.xml");
System.out.println(urls.toExternalForm());
digester.parse(urls.toExternalForm());
}
public void print() {
if (manList != null && manList.size() > 0) {
for (Mans mans : manList) {
System.out.println(mans.toString());
}
} else {
System.out.println("manList is zerro");
}
}
public static void main(String[] args) throws IOException, SAXException {
DigesterTest test = new DigesterTest();
test.xmlParse();
test.print();
// File file = new
// File("/home/test/work/DigesterTest/source/test.xml");
//
// System.out.println(file.toURI().toURL().toExternalForm());
// System.out.println(file.toURI().toURL().toString());
//
// File file = new
// File("/home/test/work/DigesterTest/source/test.xml");
// URL url = file.toURL();
// URLConnection connection = url.openConnection();
// connection.setUseCaches(false);
// InputStream stream = connection.getInputStream();
// InputSource source = new InputSource(stream);
// source.setSystemId(url.toExternalForm());
}
}