public void buildXML() { Element root = new Element("ChannelInfo"); doc = new org.jdom.Document(root); // costructring descriptor Element desc = new Element("desc"); Element name = (new Element("name")).setText(getName()); Element topicitem = (new Element("topic")).setText(getTopic()); Element pipeUri = (new Element("pipeID")).setText(getPipeUri()); desc.addContent(name); desc.addContent(pipeUri); desc.addContent(topicitem); desc.addContent(mkTimeStampXML()); desc.addContent(mkFounderXML(founderName, founderAddress, founderPeerID)); root.addContent(desc); // end descriptor Element userList = new Element("userlist"); Set<Entry<String, UserInfo>> entrySet = chUsers.entrySet(); for (Entry<String, UserInfo> e : entrySet) { UserInfo val = e.getValue(); String s = new Integer(val.getStatus()).toString(); Element user = mkUserInfoXML(val.getName(), val.getAddress(), val.getPeerID(), s); userList.addContent(user); } root.addContent(userList); }
public void parse(String s) throws JDOMException, IOException { docIn = new StringReader(s); System.out.println(s); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(docIn); Element root = doc.getRootElement(); // getting channel descriptor and rebuilding it Element chDesc = root; // .getChild("desc"); // getting Channel Name // FIXME: there's a bug ...this a workaround. Element chName = chDesc.getChild("name"); if (chName == null) { chDesc = root.getChild("desc"); chName = chDesc.getChild("name"); } channelName = new String(chName.getText()); // getting channel topic Element chTopic = chDesc.getChild("topic"); Element pipeUri = chDesc.getChild("pipeID"); pipeURI = new String(pipeUri.getText()); channelTopic = new String(chTopic.getText()); Element chDateTime = chDesc.getChild("timestamp"); nanotime = Long.parseLong(chDateTime.getText().trim()); Element chFounder = chDesc.getChild("founder"); if (chFounder == null) return; founderName = chFounder.getChildText("name"); founderAddress = chFounder.getChildText("email"); founderPeerID = chFounder.getChildText("peerid"); Element userListItem = root.getChild("userlist"); if (userListItem == null) return; List userList = userListItem.getChildren(); for (Object o : userList) { Element e = (Element) o; UserInfo infoItem = mkUserInfo(e); chUsers.put(infoItem.getAddress(), infoItem); } }