Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Saving as an XML document
Saving as an XML document [message #175878] Wed, 06 April 2005 19:04 Go to next message
Eclipse UserFriend
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 #175888 is a reply to message #175878] Wed, 06 April 2005 19:18 Go to previous messageGo to next message
Brian Fernandes is currently offline Brian FernandesFriend
Messages: 68
Registered: July 2009
Member
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 #176172 is a reply to message #175888] Fri, 08 April 2005 12:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gary.frederick.jsoft.com

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 #176262 is a reply to message #176172] Fri, 08 April 2005 18:48 Go to previous messageGo to next message
Brian Fernandes is currently offline Brian FernandesFriend
Messages: 68
Registered: July 2009
Member
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 #176375 is a reply to message #176262] Sat, 09 April 2005 18:43 Go to previous messageGo to next message
benedict heal is currently offline benedict healFriend
Messages: 28
Registered: July 2009
Junior Member
I always use XStream for turning arbitrary Java objects into XML, when
the XML does not have to be very human readable. I find it an excellent
solution - far superior to XMLEncoder in the java library. You might
find it worth a look.
Benedict


Brian Fernandes wrote:
> 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.
>
Re: Saving as an XML document [message #185439 is a reply to message #175878] Sat, 25 June 2005 06:46 Go to previous message
Ralph Soika is currently offline Ralph SoikaFriend
Messages: 193
Registered: July 2009
Senior Member
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
>
Previous Topic:GEFrc3,eclipse RC3:draw2d can not get the correct result?
Next Topic:MultiPageEditor tabs + DirectEdit
Goto Forum:
  


Current Time: Tue Dec 03 08:46:44 GMT 2024

Powered by FUDForum. Page generated in 0.03993 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top