/** * Returns the string value of the given XML node object, depending on its type. * * @param object * @return String */ public static String objectToString(Object object) { if (object instanceof Element) { return ((Element) object).getText(); } else if (object instanceof Attribute) { return ((Attribute) object).getValue(); } else if (object instanceof Text) { return ((Text) object).getText(); } else if (object instanceof CDATA) { return ((CDATA) object).getText(); } else if (object instanceof Comment) { return ((Comment) object).getText(); } else if (object instanceof Double) { return String.valueOf(object); } else if (object instanceof Boolean) { return String.valueOf(object); } else if (object instanceof String) { return (String) object; } else if (object != null) { logger.error("Unknown object type: {}", object.getClass().getName()); return null; } else { return null; } }
/** * 音乐消息对象转换成xml * * @param musicMessage * 音乐消息对象 * @return xml */ public static String musicMessageToXml(MusicMessage message) { Document document = new Document(); Element root = new Element("xml"); Element music = new Element("Music"); music.addContent(new Element("Title").setContent(new CDATA(message.getMusic().getTitle()))); music.addContent(new Element("Description").setContent(new CDATA(message.getMusic().getDescription()))); music.addContent(new Element("MusicUrl").setContent(new CDATA(message.getMusic().getMusicUrl()))); music.addContent(new Element("HQMusicUrl").setContent(new CDATA(message.getMusic().getHQMusicUrl()))); root.addContent(new Element("ToUserName").setContent(new CDATA(message.getToUserName()))); root.addContent(new Element("FromUserName").setContent(new CDATA(message.getFromUserName()))); root.addContent(new Element("CreateTime").setContent(new CDATA(String.valueOf(message.getCreateTime())))); root.addContent(new Element("MsgType").setContent(new CDATA(message.getMsgType()))); root.addContent(new Element("FuncFlag").setContent(new CDATA(String.valueOf(message.getFuncFlag())))); root.addContent(music); document.addContent(root); XMLOutputter out = new XMLOutputter(Format.getRawFormat().setOmitDeclaration(true)); return out.outputString(document); }
private static void setValue(Element e, Object v, String name) { if (v instanceof Map) e.addContent(msg2xml(v, name)); else if (v instanceof List) e.addContent(msg2xml(v, name)); else { Element n = new Element(name); if (v instanceof Number || v instanceof Boolean) n.addContent(String.valueOf(v)); else { n.addContent(new CDATA(String.valueOf(v))); } e.addContent(n); } }
private void writeChildren(TreeItem<Clip> parentTreeItem, Element parentElement) { List<TreeItem<Clip>> treeItems = parentTreeItem.getChildren(); for (TreeItem<Clip> treeItem : treeItems) { if (treeItem.getValue().isGroup()) { Element groupElement = new Element("group"); groupElement.setAttribute("name", treeItem.getValue().getName()); writeChildren(treeItem, groupElement); parentElement.addContent(groupElement); } else { Element clipElement = new Element("clip"); parentElement.addContent(clipElement); Element nameElement = new Element("name"); clipElement.addContent(nameElement); nameElement.setText(treeItem.getValue().getName()); Element contentElement = new Element("content"); clipElement.addContent(contentElement); contentElement.setContent(new CDATA(treeItem.getValue().getContent())); } } }
/** * 将微信XML转换化DBO * * @param xml * MESSAGE XML * @return DBO */ public static BasicDBObject xml2dbo(Document xml) { if (xml == null) return null; List<Element> params = xml.getRootElement().getChildren(); if (params == null || params.size() <= 0) return null; //log.info(xml2str(xml)); BasicDBObject dbo = new BasicDBObject(); for (Element e : params) { List<Content> cs = e.getContent(); if (cs != null && cs.size() > 0) { for (Content c : cs) { if (c != null) { Object o = null; switch (c.getCType()) { case Text: case CDATA: o = c.getValue(); break; default:; } if (!CommonUtil.isEmpty(o)) { if (e.getName().equalsIgnoreCase("createtime")) o = Integer.parseInt(o.toString()); dbo.append(e.getName(), o); } } } } } return dbo; }
private Element createElement(Item item) { Element element = new Element("item"); if (item.getUid() != null) { element.setAttribute(new Attribute("uid", item.getUid())); } if (item.getArg() != null) { element.setAttribute(new Attribute("arg", item.getArg())); } if (item.getType() != null) { element.setAttribute(new Attribute("arg", item.getType().getName())); } else { element.setAttribute(new Attribute("arg", Type.FILE.getName())); } if (item.getTitle() != null) { element.addContent(new Element("title").setText(item.getTitle())); } if (item.getSubTitle() != null) { element.addContent(new Element("subtitle").setText(item.getSubTitle())); } if (item.getIcon() != null && item.getIconType() != null) { Element iconElement = new Element("icon"); iconElement.setAttribute(new Attribute("type", item.getIconType().getName())); iconElement.setContent(new CDATA(item.getIcon())); element.addContent(iconElement); } return element; }
protected Element generateCDATAElement(String name, String value) { Element element = new Element(name, CONTENT_NS); CDATA cdata = new CDATA(value); element.addContent(cdata); return element; }
protected Element generateCDATAElement(final String name, final String value) { final Element element = new Element(name, CONTENT_NS); final CDATA cdata = new CDATA(value); element.addContent(cdata); return element; }
public void testTrimContents() { Content whitespace = new Text(" \n\t\t"); Content cdata = new CDATA("Test"); List<Content> content; List newList; // Empty content list content = new ArrayList<>(); assertEquals(0, AtomParserUtils.trimContents(content).size()); // Whitespace content = new ArrayList<>(1); content.add(whitespace); assertEquals(0, AtomParserUtils.trimContents(content).size()); // Two whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(whitespace); assertEquals(0, AtomParserUtils.trimContents(content).size()); // CDATA alone content = new ArrayList<>(1); content.add(cdata); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // Whitespace + CDATA content = new ArrayList<>(2); content.add(whitespace); content.add(cdata); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // CDATA + whitespace content = new ArrayList<>(2); content.add(cdata); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // CDATA surrounded with whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(cdata); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(1, newList.size()); assertTrue(newList.get(0) == cdata); // Two CDATA's surrounded with couples of whitespaces content = new ArrayList<>(2); content.add(whitespace); content.add(whitespace); content.add(cdata); content.add(cdata); content.add(whitespace); content.add(whitespace); newList = AtomParserUtils.trimContents(content); assertEquals(2, newList.size()); assertTrue(newList.get(0) == cdata); assertTrue(newList.get(1) == cdata); }
public void setCdataValue(String value) { if (value != null) { element.setContent(new CDATA(value)); } }