Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Sapphire » XmlElement CDATA section
XmlElement CDATA section [message #814562] Tue, 06 March 2012 15:50 Go to next message
Greg Amerson is currently offline Greg AmersonFriend
Messages: 119
Registered: March 2010
Senior Member
Hey everyone,

I have an xml file where I have a json String that is embedded in a xml element, like
<metadata>
   <![CDATA[{"xy":[50,50]}]]>
</metadata>


So what I'm currently doing is creating a ElementBindingImpl that will grab the XmlElement and read the contents of the CDATA section. Reading works fine, but the problem I'm having is when there is not a <metadata> element and I have to create one and then add a CDATA section. Is it possible to create a CDATA child node for a XmlElement
Re: XmlElement CDATA section [message #814775 is a reply to message #814562] Tue, 06 March 2012 22:00 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
You would have to get the DOM node from the XmlElement convenience wrapper and use DOM API to create the CDATA section. Something like this:

XmlElement el = ...
Element domel = el.getDomNode();
CDATASection cdata = domel.getDocument().createCDATASection( "..." );
domel.insertBefore( cdata, null );
Re: XmlElement CDATA section [message #814895 is a reply to message #814775] Wed, 07 March 2012 01:27 Go to previous messageGo to next message
Greg Amerson is currently offline Greg AmersonFriend
Messages: 119
Registered: March 2010
Senior Member
Easy enough, thanks!
Re: XmlElement CDATA section [message #1062726 is a reply to message #814895] Mon, 10 June 2013 14:09 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Could you please share your solution? I'm not sure where to place this wrapper code. Unfortunately, I couldn't find any CDATA example in the sample project. Thank you!
Re: XmlElement CDATA section [message #1063022 is a reply to message #1062726] Tue, 11 June 2013 20:00 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
To bind to CDATA, you will need to implement XmlValueBindingImpl and use @CustomXmlValueBinding to attach it to the property. Take a look at the XmlValueBindingImpl API. The methods that you need to implement are read() and write(). Use xml() method to get a handle on XmlElement. Then see the code snippet shown previously for how to get access to DOM and manipulate CDATA nodes. See DOM API for more details on handling CDATA.
Re: XmlElement CDATA section [message #1063118 is a reply to message #1063022] Wed, 12 June 2013 11:50 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Konstantin,

thank you for the information. Here is my solution if anyone else should have the same issue.

import org.eclipse.sapphire.modeling.xml.XmlElement;
import org.eclipse.sapphire.modeling.xml.XmlValueBindingImpl;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class CDataXmlValueBindingImpl extends XmlValueBindingImpl {

    @Override
    public String read() {
        return xml(false).getText();
    }

    @Override
    public void write(String value) {
        XmlElement el = xml(false);
        Element domel = el.getDomNode();
        this.removeAllChildren(domel);
        Node newNode = getNode(value, domel);
        domel.insertBefore(newNode, null);
    }

    private void removeAllChildren(Element domel) {
        while (domel.hasChildNodes()) {
            domel.removeChild(domel.getFirstChild());
        }
    }

    private Node getNode(String value, Element domel) {
        if (!value.isEmpty()) {
            return domel.getOwnerDocument().createCDATASection(value);
        }

        return domel.getOwnerDocument().createTextNode(value);
    }
}


And here the property annotation:

...
@CustomXmlValueBinding(impl = CDataXmlValueBindingImpl.class)
ValueProperty PROP_SOURCE_CODE = new ValueProperty(TYPE, "SourceCode");
...


Thank you!

Kon
Previous Topic:Action Handler to move value binding to top/bottom of list
Next Topic:License of the icons
Goto Forum:
  


Current Time: Fri Nov 08 22:43:39 GMT 2024

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

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

Back to the top