2019独角兽企业重金招聘Python工程师标准>>> ![hot3.png](https://img7.php1.cn/3cdc5/cf6c/807/96f8bdd059ecb57d.png)
直接上代码
public static void createXML(String title,String keyword,String summary,String videoFormat,String videoUrl,String logoUrl) throws ParserConfigurationException, TransformerException, FileNotFoundException {DocumentBuilderFactory documentBuilderFactory &#61; DocumentBuilderFactory.newInstance();DocumentBuilder builder &#61; documentBuilderFactory.newDocumentBuilder();Document document &#61; builder.newDocument();//构建xml节点//CMSContentInfoElement root &#61; document.createElement("CMSContentInfo");//EntityDataElement entityDataElement &#61; document.createElement("EntityData");//按照顺序添加各个节点document.appendChild(root);//添加EntityDataroot.appendChild(entityDataElement);//数组赋值Map map1 &#61; new HashMap();map1.put("title","title");map1.put("titleValue",title);map1.put("keyword","keyword");map1.put("keywordValue",keyword);map1.put("summary","summary");map1.put("summaryValue",summary);String itemCode &#61; "";String itemName &#61; "";String value &#61; "";//AttributeItemfor (int i&#61;0;i<3;i&#43;&#43;){if (i&#61;&#61;0){itemCode &#61; (String) map1.get("title");itemName &#61; (String) map1.get("title");value &#61; (String) map1.get("titleValue");}else if (i&#61;&#61;1){itemCode &#61; (String) map1.get("keyword");itemName &#61; (String) map1.get("keyword");value &#61; (String) map1.get("keywordValue");}else {itemCode &#61; (String) map1.get("summary");itemName &#61; (String) map1.get("summary");value &#61; (String) map1.get("summaryValue");} //创建节点Element attributeItemElement &#61; document.createElement("AttributeItem");Element itemCodeElement &#61; document.createElement("ItemCode");Text itemCodeValue &#61; document.createTextNode(itemCode);Element itemNameElement &#61; document.createElement("ItemName");Text itemNameValue &#61; document.createTextNode(itemName);Element valueElement &#61; document.createElement("Value");Text valueValue &#61; document.createTextNode(value);//赋值entityDataElement.appendChild(attributeItemElement);attributeItemElement.appendChild(itemCodeElement);itemCodeElement.appendChild(itemCodeValue);attributeItemElement.appendChild(itemNameElement);itemNameElement.appendChild(itemNameValue);attributeItemElement.appendChild(valueElement);valueElement.appendChild(valueValue);}//ContentFileElement contentFileElement &#61; document.createElement("ContentFile");//FileItemElement fileItemElement &#61; document.createElement("FileItem");//FileTypeIDElement fileTypeIdElement &#61; document.createElement("FileTypeID");Text fileTypeIdValue &#61; document.createTextNode(videoFormat);//FileURLElement fileURLElement &#61; document.createElement("FileURL");Text fileURLValue &#61; document.createTextNode(videoUrl);//添加ContentFileroot.appendChild(contentFileElement);//添加FileItemcontentFileElement.appendChild(fileItemElement);//添加FileTypeIDfileItemElement.appendChild(fileTypeIdElement);//赋值fileTypeIdElement.appendChild(fileTypeIdValue);//添加FileURLfileItemElement.appendChild(fileURLElement);//赋值fileURLElement.appendChild(fileURLValue);//KeyFrameElement keyFrameElement &#61; document.createElement("KeyFrame");Element keyFrameItemElement &#61; document.createElement("KeyFrameItem");Element keyFramePathElement &#61; document.createElement("KeyFramePath");Text keyFramePathValue &#61; document.createTextNode(logoUrl);//添加KeyFrameroot.appendChild(keyFrameElement);keyFrameElement.appendChild(keyFrameItemElement);keyFrameItemElement.appendChild(keyFramePathElement);//赋值keyFramePathElement.appendChild(keyFramePathValue);//转换Transformer transformer &#61; TransformerFactory.newInstance().newTransformer();//设置换行和缩进transformer.setOutputProperty(OutputKeys.INDENT,"yes");transformer.setOutputProperty(OutputKeys.METHOD, "xml");Source source &#61; new DOMSource(document);Result result &#61; new StreamResult(new FileOutputStream(new File("e:/test.xml")));transformer.transform(source,result);}public static void main(String[] args) throws Exception {createXML("标题","关键字","描述","视频格式","视频地址","logo地址");}
测试结果如下
titletitle标题keywordkeyword关键字summarysummary描述视频格式视频地址logo地址