XmlElement CDATA section [message #814562] |
Tue, 06 March 2012 10:50  |
Eclipse User |
|
|
|
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 #1063118 is a reply to message #1063022] |
Wed, 12 June 2013 07:50  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.02314 seconds