下面的代码简单的展示了如何使用Jdom读取保存在字符串中的xml流。EventSpaceParser 类提供一个静态的parse方法,来解析出xml流中的EventSpace对象,调用时只需提供一个String就可以了,本例中用StringBuffer构造了一个xml流。EventSpace类比较简单,只有一些get方法和set方法,略去。
附件中提供一个jdom jar包,在项目中需要包含这个jar包。
public class EventSpaceParser {
public static EventSpace parse(String eventSpace) {
EventSpace es = new EventSpace();
Element root = getRootElement(eventSpace);
setEventSpace(root, es);
return es;
}
private static Element getRootElement(String eventSpace) {
[color=darkblue]SAXBuilder builder = new SAXBuilder(false);
Document doc = null;
Reader in = new StringReader(eventSpace);[/color]
try {
doc = builder.build(in);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc.getRootElement();
}
private static void setEventSpace(Element root, EventSpace es) {
es.setDate(root.getChild(date).getText());
es.setEsID(root.getChild(esID).getText());
es.setOwner(root.getChild(owner).getChild(avatarName).getText());
es.setStatus(root.getChild(status).getText());
}
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<eventspace>");
stringBuffer.append("<date>");
stringBuffer.append("2008-12-18");
stringBuffer.append("</date>");
stringBuffer.append("<esID>");
stringBuffer.append("92a4f007-ff1d-4053-8753-e078d1c53ff5");
stringBuffer.append("</esID>");
stringBuffer.append("<owner>");
stringBuffer.append("<avatarName>");
stringBuffer.append("Matthias Hub(Germany)");
stringBuffer.append("</avatarName>");
stringBuffer.append("<id>");
stringBuffer.append("5db385b0-e504-4aa2-9ddc-3fe95f6d6dc1");
stringBuffer.append("</id>");
stringBuffer.append("</owner>");
stringBuffer.append("<status>");
stringBuffer.append("ACTIVE");
stringBuffer.append("</status>");
stringBuffer.append("</eventspace>");
EventSpace es = parse(stringBuffer.toString());
es.print();
}
private static final String date = "date";
private static final String owner = "owner";
private static final String avatarName = "avatarName";
private static final String status = "status";
private static final String esID = "esID";
}
附件中提供一个jdom jar包,在项目中需要包含这个jar包。
public class EventSpaceParser {
public static EventSpace parse(String eventSpace) {
EventSpace es = new EventSpace();
Element root = getRootElement(eventSpace);
setEventSpace(root, es);
return es;
}
private static Element getRootElement(String eventSpace) {
[color=darkblue]SAXBuilder builder = new SAXBuilder(false);
Document doc = null;
Reader in = new StringReader(eventSpace);[/color]
try {
doc = builder.build(in);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc.getRootElement();
}
private static void setEventSpace(Element root, EventSpace es) {
es.setDate(root.getChild(date).getText());
es.setEsID(root.getChild(esID).getText());
es.setOwner(root.getChild(owner).getChild(avatarName).getText());
es.setStatus(root.getChild(status).getText());
}
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<eventspace>");
stringBuffer.append("<date>");
stringBuffer.append("2008-12-18");
stringBuffer.append("</date>");
stringBuffer.append("<esID>");
stringBuffer.append("92a4f007-ff1d-4053-8753-e078d1c53ff5");
stringBuffer.append("</esID>");
stringBuffer.append("<owner>");
stringBuffer.append("<avatarName>");
stringBuffer.append("Matthias Hub(Germany)");
stringBuffer.append("</avatarName>");
stringBuffer.append("<id>");
stringBuffer.append("5db385b0-e504-4aa2-9ddc-3fe95f6d6dc1");
stringBuffer.append("</id>");
stringBuffer.append("</owner>");
stringBuffer.append("<status>");
stringBuffer.append("ACTIVE");
stringBuffer.append("</status>");
stringBuffer.append("</eventspace>");
EventSpace es = parse(stringBuffer.toString());
es.print();
}
private static final String date = "date";
private static final String owner = "owner";
private static final String avatarName = "avatarName";
private static final String status = "status";
private static final String esID = "esID";
}