Rank: Newbie
Joined: 12/19/2008 Posts: 2
|
Is there a way, using the 2007 R3 XML Spy generated code (both Java and C++) to insert an XML comment.
It looks like it might have been there in earlier versions of the generated code, but with the current move away from CDoc it no longer appears to be operational.
Thanks, Caleb
|
Rank: Newbie
Joined: 10/28/2002 Posts: 1,283 Location: AT
|
In order to create a comment in the code you will have to access the DOM, create the comment node with the DOM and then insert / append the comment in the correct place (also using the DOM). For example assuming that you generate java code from the following schema named example.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b"> <xs:complexType> <xs:sequence> <xs:element name="c" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
If we create a new xml document and want to append a comment after the child element "c". We would do the following:
com.example.example2 doc = com.example.example2.createDocument(); com.example.aType root = doc.a.append(); com.example.bType b = root.b.append(); b.c.append().setValue("this is the element c");
/* use getNode to get the DOM node and appendChild to append this to the DOM structure. the parameter passed to this method must be a new comment node which is created by accessing the OwnerDocument method */
b.getNode().appendChild(b.getNode().getOwnerDocument().createComment("this is a comment"));
doc.saveToFile("c://23146//example1.xml", true);
|