Saving as an XML document [message #175878] |
Wed, 06 April 2005 15:04  |
Eclipse User |
|
|
|
Originally posted by: jdelgad.correo.ugr.es
Hi everyone,
I would like to modify the doSave method in order to save the diagram
as an xml document. How can I do this?
Thank you
--Jose
|
|
|
|
|
Re: Saving as an XML document [message #176262 is a reply to message #176172] |
Fri, 08 April 2005 14:48   |
Eclipse User |
|
|
|
Gary,
You can't really compare JDOM and JAXP :)
I think this artcle explains it rather nicely (rather dated, but still
mostly valid)
http://www-106.ibm.com/developerworks/xml/library/x-jaxp/
Hope this helps,
Brian.
"Gary Frederick" <gary.frederick@jsoft.com> wrote in message
news:d35uh3$mk3$1@news.eclipse.org...
> Howdy Brian and all,
>
> I'm interested in this also.
>
> You suggested using JDom. I looked over some of the ways to save an XML
> document (including JDom) and picked JAXP 1.3. I'm using it because it
> comes with J2SE 5.0 and includes the recent XML and XSLT stuff from
> Apache. My work uses XSLT to transform the XML.
>
> Did I choose poorly?
>
> Thanks,
>
> Gary
>
> Brian Fernandes wrote:
>> Jose,
>>
>> There are many ways to do this - and you should choose a solution
>> depending on the complexity of your generation requirements.
>>
>> To get you started however, you can use JDom.
>>
>> Build up the DOM in your model - using classes like org.jdom.Element
>> The Element class will allow you to add attributes and child Elements.
>>
>> Use XMLOutputter#outputString on the root element of your Tree to get the
>> XML string, which you can save to a file.
>>
>> More complex solutions could involve using XSTL or maybe Velocity and
>> templates for XML generation from your model, not really necessary if the
>> persistant information is not too complex.
>>
>> Hope this helps,
>> Brian.
>>
>> "Jose" <jdelgad@correo.ugr.es> wrote in message
>> news:0cd2adc6f17901bd7a0c5412b63ab472$1@www.eclipse.org...
>>
>>>Hi everyone,
>>>
>>> I would like to modify the doSave method in order to save the diagram
>>> as an xml document. How can I do this?
>>>
>>>Thank you
>>>
>>> --Jose
>>>
>>
>>
|
|
|
|
Re: Saving as an XML document [message #185439 is a reply to message #175878] |
Sat, 25 June 2005 02:46  |
Eclipse User |
|
|
|
hi,
you can do this in the doSave() Methode very simple by using the standard
XML APIs.
Here is s short example:
/****************************************************/
import java.io.ByteArrayOutputStream;
import org.eclipse.core.resources.IFile;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
......
public void doSave(IProgressMonitor monitor) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLModelParser.saveModel(out);
// create XML DOM Tree
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document doc = domBuilder.newDocument();
// Insert the root element node
Element element = doc.createElement("root");
doc.appendChild(element);
// Insert a comment in front of the element node
Comment comment = doc.createComment("a comment");
doc.insertBefore(comment, element);
// Add a text node to the element
element.appendChild(doc.createTextNode("D"));
// Add a text node to the beginning of the element
element.insertBefore(doc.createTextNode("A"),
element.getFirstChild());
// Add a text node before the last child of the element
element.insertBefore(doc.createTextNode("C"),
element.getLastChild());
// Add another element after the first child of the root element
Element element2 = doc.createElement("item");
element.insertBefore(element2,
element.getFirstChild().getNextSibling());
// Add a text node in front of the new item element
element2.getParentNode().insertBefore(doc.createTextNode("B "),
element2);
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
Result result = new StreamResult(out);
// Write the DOM document to the outputstream
Transformer transformer =
TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
// save outputstream to IFile....
IFile file = ((IFileEditorInput)getEditorInput()).getFile();
file.setContents(new ByteArrayInputStream(out.toByteArray()),
true, false, monitor);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
/*******************************************/
Ralph
"Jose" <jdelgad@correo.ugr.es> schrieb im Newsbeitrag
news:0cd2adc6f17901bd7a0c5412b63ab472$1@www.eclipse.org...
> Hi everyone,
>
> I would like to modify the doSave method in order to save the diagram as
> an xml document. How can I do this?
>
> Thank you
>
> --Jose
>
|
|
|
Powered by
FUDForum. Page generated in 0.23458 seconds