Can I add a comment line to a child node?
I need to build a XML file with the below format:
<PartNumber>
<Code>
<Parameter>
<ParameterCode> XYOperating Time</ParameterCode>
<UnitOfMeasure>HR</UnitOfMeasure>
<Details>
<StartMeasure>NEW</StartMeasure>
<TotalValue>62.0</TotalValue>
<Date>2009-12-21T10:33:36-02:00</Date>
<!--<Comments>
<DATE>2009-12-21-12T10:33:36-02:33:36</DATE><TEXT> Total Testing performed during Test Procedure/TEXT>
</Comments -->
</Details>
</Parameter>
</Code>
</PartNumber>
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();
StreamRe开发者_如何学JAVAsult 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?
Have you tried creating a Comment
node and appending it to your tree?
精彩评论