Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » Create a WTP XML Node from an XML string
Create a WTP XML Node from an XML string [message #228524] Mon, 02 March 2009 14:49 Go to next message
Istvan is currently offline IstvanFriend
Messages: 15
Registered: July 2009
Junior Member
Hello,

I'm working on an XML editor based on WTP XML Editor, so I have to work with
a WTP DOM.

The problem:
I have to create a Node from an XML String and insert the node to the WTP
DOM.
I can create document from a String but it won't be a WTP Document, so when
I try to insert the first child of the newly created DOM to the WTP DOM I
got the following exception:

java.lang.ClassCastException: org.apache.xerces.dom.DeferredElementImpl
cannot be cast to org.eclipse.wst.xml.core.internal.document.NodeImpl
at
org.eclipse.wst.xml.core.internal.document.NodeContainer.ins ertBefore(NodeContainer.java:269)
at
org.eclipse.wst.xml.core.internal.document.ElementImpl.inser tBefore(ElementImpl.java:693)
at
org.eclipse.wst.xml.core.internal.document.NodeContainer.app endChild(NodeContainer.java:129)

Code I use to create the DOM:
public static Document stringToDom(String xmlSource) throws SAXException,
ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}

I browsed the wst.core code but can't find any solution.

Can anybody help me?

Thanks,
Istvan
Re: Create a WTP XML Node from an XML string [message #228540 is a reply to message #228524] Mon, 02 March 2009 15:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dcarver.starstandard.org

Have you tried the cloneNode() method?

I think the problem you are going to run into though is that you are
going from two different DOM implementations. So what you might have to
do is create a custom Clone Node method that takes the input of the
Xerces DOM node, and recreates a WTP DOM node, and returns the WTP DOM
node as it's result.

Unfortunately, WTP DOM is known not to be a 100% complete implementation
of DOM, so there are edge cases where it has problems. This appears to
be one.

Dave

Istvan wrote:
> Hello,
>
> I'm working on an XML editor based on WTP XML Editor, so I have to work
> with a WTP DOM.
>
> The problem:
> I have to create a Node from an XML String and insert the node to the
> WTP DOM.
> I can create document from a String but it won't be a WTP Document, so
> when I try to insert the first child of the newly created DOM to the WTP
> DOM I
> got the following exception:
>
> java.lang.ClassCastException: org.apache.xerces.dom.DeferredElementImpl
> cannot be cast to org.eclipse.wst.xml.core.internal.document.NodeImpl
> at
> org.eclipse.wst.xml.core.internal.document.NodeContainer.ins ertBefore(NodeContainer.java:269)
>
> at
> org.eclipse.wst.xml.core.internal.document.ElementImpl.inser tBefore(ElementImpl.java:693)
>
> at
> org.eclipse.wst.xml.core.internal.document.NodeContainer.app endChild(NodeContainer.java:129)
>
>
> Code I use to create the DOM:
> public static Document stringToDom(String xmlSource) throws
> SAXException, ParserConfigurationException, IOException {
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = factory.newDocumentBuilder();
> return builder.parse(new InputSource(new StringReader(xmlSource)));
> }
>
> I browsed the wst.core code but can't find any solution.
>
> Can anybody help me?
>
> Thanks,
> Istvan
Re: Create a WTP XML Node from an XML string [message #228548 is a reply to message #228540] Mon, 02 March 2009 16:08 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
David,

I suppose one could create a WTP DOM Document and use importNode on the
root element to clone, or would that result in cast exceptions as well?


David Carver wrote:
> Have you tried the cloneNode() method?
>
> I think the problem you are going to run into though is that you are
> going from two different DOM implementations. So what you might have
> to do is create a custom Clone Node method that takes the input of the
> Xerces DOM node, and recreates a WTP DOM node, and returns the WTP DOM
> node as it's result.
>
> Unfortunately, WTP DOM is known not to be a 100% complete
> implementation of DOM, so there are edge cases where it has problems.
> This appears to be one.
>
> Dave
>
> Istvan wrote:
>> Hello,
>>
>> I'm working on an XML editor based on WTP XML Editor, so I have to
>> work with a WTP DOM.
>>
>> The problem:
>> I have to create a Node from an XML String and insert the node to the
>> WTP DOM.
>> I can create document from a String but it won't be a WTP Document,
>> so when I try to insert the first child of the newly created DOM to
>> the WTP DOM I
>> got the following exception:
>>
>> java.lang.ClassCastException:
>> org.apache.xerces.dom.DeferredElementImpl cannot be cast to
>> org.eclipse.wst.xml.core.internal.document.NodeImpl
>> at
>> org.eclipse.wst.xml.core.internal.document.NodeContainer.ins ertBefore(NodeContainer.java:269)
>>
>> at
>> org.eclipse.wst.xml.core.internal.document.ElementImpl.inser tBefore(ElementImpl.java:693)
>>
>> at
>> org.eclipse.wst.xml.core.internal.document.NodeContainer.app endChild(NodeContainer.java:129)
>>
>>
>> Code I use to create the DOM:
>> public static Document stringToDom(String xmlSource) throws
>> SAXException, ParserConfigurationException, IOException {
>> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
>> DocumentBuilder builder = factory.newDocumentBuilder();
>> return builder.parse(new InputSource(new StringReader(xmlSource)));
>> }
>>
>> I browsed the wst.core code but can't find any solution.
>>
>> Can anybody help me?
>>
>> Thanks,
>> Istvan


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create a WTP XML Node from an XML string [message #228556 is a reply to message #228548] Mon, 02 March 2009 16:43 Go to previous messageGo to next message
Istvan is currently offline IstvanFriend
Messages: 15
Registered: July 2009
Junior Member
Thanks for quick replies.

I checked this just now, normal cloneNode does not work, it produces the
same Exception than without using cloneNode.
Custom cloning: I thought there is a "more standard" way than doing this but
I think I will choose it, it is sure it will work.

Although, I would give a chance to the other solution.

Ed, how can I create a WTP DOM?
Can I force the stringToDom method below to create a WTP DOM?

Istvan

"Ed Merks" <Ed.Merks@gmail.com> wrote in message
news:goh09i$4bl$1@build.eclipse.org...
> David,
>
> I suppose one could create a WTP DOM Document and use importNode on the
> root element to clone, or would that result in cast exceptions as well?
>
>
> David Carver wrote:
>> Have you tried the cloneNode() method?
>>
>> I think the problem you are going to run into though is that you are
>> going from two different DOM implementations. So what you might have to
>> do is create a custom Clone Node method that takes the input of the
>> Xerces DOM node, and recreates a WTP DOM node, and returns the WTP DOM
>> node as it's result.
>>
>> Unfortunately, WTP DOM is known not to be a 100% complete implementation
>> of DOM, so there are edge cases where it has problems. This appears to
>> be one.
>>
>> Dave
>>
>> Istvan wrote:
>>> Hello,
>>>
>>> I'm working on an XML editor based on WTP XML Editor, so I have to work
>>> with a WTP DOM.
>>>
>>> The problem:
>>> I have to create a Node from an XML String and insert the node to the
>>> WTP DOM.
>>> I can create document from a String but it won't be a WTP Document, so
>>> when I try to insert the first child of the newly created DOM to the WTP
>>> DOM I
>>> got the following exception:
>>>
>>> java.lang.ClassCastException: org.apache.xerces.dom.DeferredElementImpl
>>> cannot be cast to org.eclipse.wst.xml.core.internal.document.NodeImpl
>>> at
>>> org.eclipse.wst.xml.core.internal.document.NodeContainer.ins ertBefore(NodeContainer.java:269)
>>> at
>>> org.eclipse.wst.xml.core.internal.document.ElementImpl.inser tBefore(ElementImpl.java:693)
>>> at
>>> org.eclipse.wst.xml.core.internal.document.NodeContainer.app endChild(NodeContainer.java:129)
>>>
>>> Code I use to create the DOM:
>>> public static Document stringToDom(String xmlSource) throws
>>> SAXException, ParserConfigurationException, IOException {
>>> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
>>> DocumentBuilder builder = factory.newDocumentBuilder();
>>> return builder.parse(new InputSource(new StringReader(xmlSource)));
>>> }
>>>
>>> I browsed the wst.core code but can't find any solution.
>>>
>>> Can anybody help me?
>>>
>>> Thanks,
>>> Istvan
Re: Create a WTP XML Node from an XML string [message #228565 is a reply to message #228556] Mon, 02 March 2009 17:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Istvan,

I assume there's a factory somewhere to create a Document. But then,
I'd have assumed there'd be something to create a instance from a Reader
or an InputStream...


Istvan wrote:
> Thanks for quick replies.
>
> I checked this just now, normal cloneNode does not work, it produces
> the same Exception than without using cloneNode.
> Custom cloning: I thought there is a "more standard" way than doing
> this but I think I will choose it, it is sure it will work.
>
> Although, I would give a chance to the other solution.
>
> Ed, how can I create a WTP DOM?
> Can I force the stringToDom method below to create a WTP DOM?
>
> Istvan
>
> "Ed Merks" <Ed.Merks@gmail.com> wrote in message
> news:goh09i$4bl$1@build.eclipse.org...
>> David,
>>
>> I suppose one could create a WTP DOM Document and use importNode on
>> the root element to clone, or would that result in cast exceptions as
>> well?
>>
>>
>> David Carver wrote:
>>> Have you tried the cloneNode() method?
>>>
>>> I think the problem you are going to run into though is that you are
>>> going from two different DOM implementations. So what you might
>>> have to do is create a custom Clone Node method that takes the input
>>> of the Xerces DOM node, and recreates a WTP DOM node, and returns
>>> the WTP DOM node as it's result.
>>>
>>> Unfortunately, WTP DOM is known not to be a 100% complete
>>> implementation of DOM, so there are edge cases where it has
>>> problems. This appears to be one.
>>>
>>> Dave
>>>
>>> Istvan wrote:
>>>> Hello,
>>>>
>>>> I'm working on an XML editor based on WTP XML Editor, so I have to
>>>> work with a WTP DOM.
>>>>
>>>> The problem:
>>>> I have to create a Node from an XML String and insert the node to
>>>> the WTP DOM.
>>>> I can create document from a String but it won't be a WTP Document,
>>>> so when I try to insert the first child of the newly created DOM to
>>>> the WTP DOM I
>>>> got the following exception:
>>>>
>>>> java.lang.ClassCastException:
>>>> org.apache.xerces.dom.DeferredElementImpl cannot be cast to
>>>> org.eclipse.wst.xml.core.internal.document.NodeImpl
>>>> at
>>>> org.eclipse.wst.xml.core.internal.document.NodeContainer.ins ertBefore(NodeContainer.java:269)
>>>>
>>>> at
>>>> org.eclipse.wst.xml.core.internal.document.ElementImpl.inser tBefore(ElementImpl.java:693)
>>>>
>>>> at
>>>> org.eclipse.wst.xml.core.internal.document.NodeContainer.app endChild(NodeContainer.java:129)
>>>>
>>>>
>>>> Code I use to create the DOM:
>>>> public static Document stringToDom(String xmlSource) throws
>>>> SAXException, ParserConfigurationException, IOException {
>>>> DocumentBuilderFactory factory =
>>>> DocumentBuilderFactory.newInstance();
>>>> DocumentBuilder builder = factory.newDocumentBuilder();
>>>> return builder.parse(new InputSource(new StringReader(xmlSource)));
>>>> }
>>>>
>>>> I browsed the wst.core code but can't find any solution.
>>>>
>>>> Can anybody help me?
>>>>
>>>> Thanks,
>>>> Istvan
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create a WTP XML Node from an XML string [message #228573 is a reply to message #228565] Mon, 02 March 2009 18:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dcarver.starstandard.org

Ed Merks wrote:
> Istvan,
>
> I assume there's a factory somewhere to create a Document. But then,
> I'd have assumed there'd be something to create a instance from a Reader
> or an InputStream...
>

There is...but it does not implement the Standard JAXP Document
builder...with that said the following code will simulate it.

public Document load(java.net.URL url) throws DOMTestLoadException {
IStructuredDocument document = null;
Document doc = null;
IStructuredModel model = null;


try {

FileInputStream file = new FileInputStream(url.getPath());
InputStream inputStream = Utilities.getMarkSupportedStream(file);
ModelHandlerForXML xmlModelHandler = new ModelHandlerForXML();
IDocumentLoader loader = xmlModelHandler.getDocumentLoader();
document =
(IStructuredDocument)loader.createNewStructuredDocument("/home/dcarver/test.xml ",
inputStream, EncodingRule.FORCE_DEFAULT);
IModelLoader xmlModelLoader = xmlModelHandler.getModelLoader();
model = xmlModelLoader.createModel(document, url.getPath(),
xmlModelHandler);

}
catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String text = model.getStructuredDocument().getText();

IDOMModel docModel = (IDOMModel)model;
doc = (Document)docModel.getDocument();

return doc;
}


That is some sample of how to create an WTP DOM from an input file.

Dave
Re: Create a WTP XML Node from an XML string [message #228581 is a reply to message #228573] Mon, 02 March 2009 20:35 Go to previous messageGo to next message
Istvan is currently offline IstvanFriend
Messages: 15
Registered: July 2009
Junior Member
In the meantime I implemented a simple cloneNode method, it works.

I also tested the code Dave posted: I changed it to receive an xml-string
and build a document from it. Inserting the result as a child of a WTP node
works, but I get errors when I try to format the node using
xmlNodeActionManager.reformat(node, true).
Maybe I will debug this later, but for now I will use the cloneNode method.

Thanks a lot for both of you.

Istvan
Re: Create a WTP XML Node from an XML string [message #228597 is a reply to message #228581] Tue, 03 March 2009 03:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dcarver.starstandard.org

Istvan,

If you can please open a bug report against the existing wst.xml
component, and report the issue with the cloneNode method.

It should take any DOM interface and clone the node, and not be dependent.

Dave

Istvan wrote:
> In the meantime I implemented a simple cloneNode method, it works.
>
> I also tested the code Dave posted: I changed it to receive an
> xml-string and build a document from it. Inserting the result as a child
> of a WTP node works, but I get errors when I try to format the node
> using xmlNodeActionManager.reformat(node, true).
> Maybe I will debug this later, but for now I will use the cloneNode method.
>
> Thanks a lot for both of you.
>
> Istvan
Re: Create a WTP XML Node from an XML string [message #228618 is a reply to message #228597] Tue, 03 March 2009 06:42 Go to previous message
Eclipse UserFriend
Originally posted by: david_williams.linux.vnet.ibm.com

David Carver wrote:
> ...
> If you can please open a bug report against the existing wst.xml
> component, and report the issue with the cloneNode method.
> ...
>
I just skimmed read the specifics of this thread, but if you have
specific problems with 'cloneNode', you might document them in
https://bugs.eclipse.org/bugs/show_bug.cgi?id=156293
Previous Topic:Password protect 3.4.1 infocenter
Next Topic:Eclipse won't start up after update
Goto Forum:
  


Current Time: Tue Mar 19 04:23:36 GMT 2024

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

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

Back to the top