我可以在子节点上添加注释行吗?

时间:2011-03-28 13:54:51

标签: java xml dom hierarchical-data

我需要使用以下格式构建XML文件:

<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>
&lt;DATE&gt;2009-12-21-12T10:33:36-02:33:36&lt;/DATE&gt;&lt;TEXT&gt;     Total Testing performed during Test Procedure/TEXT&gt;
</Comments -->


        </Details>
          </Parameter>
 </Code>
</PartNumber>

我使用了这段代码:

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;
    }   
}

我能够构建我想要的树,但我无法将注释行添加为Details的子项。

这样做是正确的还是我不允许将评论作为子元素?

1 个答案:

答案 0 :(得分:1)

您是否尝试过creating a Comment node并将其附加到树上?