Can I add a comment line to a child node?

I need to build a XML file with the below format: XYOperating Time HR
NEW 62.0 2009-12-21T10:33:36-02:00
I used this code: import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; import org.apache.xml.serializer.*; public class MakeXML { public MakeXML() { } public String doit() throws javax.xml.parsers.ParserConfigurationException, javax.xml.transform.TransformerException, javax.xml.transform.TransformerConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null,null,null); Element e1 = doc.createElement("xxx"); doc.appendChild(e1); // primo elemento e1.setAttribute("version","3.1"); e1.setAttribute("xmlns:xsi",""); // inizio descri codice Element e2 = doc.createElement("Part"); e1.appendChild(e2); // descrizione codici PartId Element e3 = doc.createElement("PartId"); e2.appendChild(e3); // MANUFACTURER Element e4 = doc.createElement("Manufacturer"); e3.appendChild(e4); Element e5= doc.createElement("ReferenceNumber"); e4.appendChild(e5); e5.setTextContent("hhhhhhhhhhhhh"); Element e6= doc.createElement("CageCode"); e4.appendChild(e6); e6.setTextContent("xxxx"); // ADDITIONALREFERENCE e4 = doc.createElement("AdditionalReference"); e3.appendChild(e4); e5 = doc.createElement("ReferenceNumber"); e4.appendChild(e5); e5.setTextContent("yyyyyyyy"); e5 = doc.createElement("CageCode"); e4.appendChild(e5); e5.setTextContent("vvvvv"); // NOMENCLATURE e4 = doc.createElement("Nomenclature"); e3.appendChild(e4); e4.setTextContent("zzzzzzzzzzzzz"); e4 = doc.createElement("ItemUniqueIdentifier"); e3.appendChild(e4); e4.setTextContent("xxxxxxxxxxxxxx"); e4 = doc.createElement("SerialNumber"); e3.appendChild(e4); e4.setTextContent("vvbb"); e4 = doc.createElement("ItemClasscode"); e2.appendChild(e4); e4.setTextContent("cc"); e4 = doc.createElement("ManufacturerDate"); e2.appendChild(e4); e4.setTextContent("2009-12-21-12T10:33:36-02:33:36"); e4 = doc.createElement("IsServiceable"); e2.appendChild(e4); e4.setTextContent("true"); e4 = doc.createElement("Iscomplete"); e2.appendChild(e4); e4.setTextContent("true"); e4 = doc.createElement("IsPreserved"); e2.appendChild(e4); e4.setTextContent("false"); //usage data //----------------------------------------------------------------------------------------- e3 = doc.createElement("Usagedata"); e2.appendChild(e3); e4 = doc.createElement("UsageParameter"); e3.appendChild(e4); e5 = doc.createElement("ParameterCode"); e4.appendChild(e5); e5.setTextContent(" OPERATING TIME"); e5 = doc.createElement("UnitOfMeasure"); e4.appendChild(e5); e5.setTextContent("HR"); //----------------------------------------------------------------------------------------- e5 = doc.createElement("UsageRecord"); e4.appendChild(e5); e6 = doc.createElement("StartMeasureEvent"); e5.appendChild(e6); e6.setTextContent("NEW"); e6 = doc.createElement("TotalValue"); e5.appendChild(e6); e6.setTextContent("62.0"); e6 = doc.createElement("DateRecorder"); e5.appendChild(e6); e6.setTextContent("2009-12-21-12T10:33:36-02:33:36"); String xxdata=" ccccdfffffffff "; Comment e9 = doc.createComment(xxdata); e5.appendChild(e9); // transform the Document into a String DISCLOSUREDATAMODIFIEDDATA DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); //transformer.setOutputProperty (OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8"); transformer.setOutputProperty(OutputKeys.VERSION,"" ); transformer.setOutputProperty(OutputKeys.STANDALONE,"yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); java.io.StringWriter sw = new java.io.StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); String xml = sw.toString(); return xml; } } I was able to built the tree I wanted, but I can't add the comment line as child of `Details`. Is correct to do this or am I not permitted to have comment as child element?
While I was formatting your post, I noticed that you're missing a close angle bracket near the end of the XML comment. Not sure if that was a typo when you were asking the question or an actual issue in your code, but now you know.

以上就是Can I add a comment line to a child node?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏