Hi,

I had such problems as I started, I do not have a proper sample but here is a code fragment that illustrates the building of a DOM tree from scratch.

This sample is in C++, I guess it can be easily converted to Java if needed.

int test002 (void) {
  int status;

  cout << "***** 002 - Creates a DOM Tree from scratch." << endl;
 
  DOMParser    dpPar;
  DOM_Document ddDoc;
  DOM_Element  deEle;
  DOM_Element  deFile;
  DOM_Text     dtTex;
  DOM_Node     dnNod;

  status = 0;
 
  dpPar.setDoValidation(false);
  try {
    dpPar.parse("none");
  }
  catch (const XMLException& toCatch) {
    cerr << "Error during parsing - Exception message is:\n" << StrX(toCatch.getMessage()) << endl;
  }

  cout << "****  Start document in parser" << endl;
  dpPar.startDocument();

  cout << "****  Get document" << endl;
  ddDoc = dpPar.getDocument();

  cout << "****  Create element PICTURE in Document" << endl;
  deEle  = ddDoc.createElement("PICTURE");
  deEle.setAttribute("Id", "f001");
  deFile = ddDoc.createElement("FILE");
  dtTex  = ddDoc.createTextNode(DOMString("picture.gif"));

  cout << "****  Get Document's root" << endl;
  dnNod = ddDoc.getDocumentElement();

  cout << "****  Append created element to root" << endl;
  ddDoc.appendChild(deEle);
  deEle.appendChild(deFile);
  deFile.appendChild(dtTex);

  cout << "****  Debug output" << endl;
  XML::debug(ddDoc);

  return status;
}

You should add this to the DOMCount program... ignore XML::debug(), it is simply a dump of the XML tree as you can see it in DOMPrint.

This code does not test anything nore manage exceptions...

Jean Georges PERRIN
--
Four J's Development Tools (www.4js.com)
[EMAIL PROTECTED] - Tel +33 (0)3 88 18 61 20 - Fax +33 (0)3 88 18 61 21
--
CAUTION: import com.fourjs.StandardDisclaimer;



> -----Original Message-----
> From: Chacon, Eric [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 13, 2000 01:50
> To: '[EMAIL PROTECTED]'
> Subject: Programmatically creating an XML file
>
>
> Apache,
>
> Do you have any examples showing how to use Xerces to create an XML file
> programmatically?  I see several showing the use of the DOM and SAX
> parser(s) but nothing that would help me to create an XML file to
> feed into
> a parser.
>
> Thanks,
>
> Eric
>

Reply via email to