You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@xml.apache.org by Simone Badoer <si...@lnl.infn.it> on 2001/09/25 19:02:45 UTC
Element from String
Hi.
I need to obtain an Element from a String in a valid XML format, for example:
Element el =xxxFunction("<TAG attr=\"abc\">\n <CHILD/>\n</TAG>");
I know the inverse function, DOMWriter.nodeToString, what's the right name for xxxFunction?
Thank you,
Simone.
Re: Element from String
Posted by Tom Myers <to...@dreamscape.com>.
At 07:02 PM 9/25/2001 +0200, Simone Badoer wrote:
>Hi.
>
>I need to obtain an Element from a String in a valid XML format, for example:
>
>Element el =xxxFunction("<TAG attr=\"abc\">\n <CHILD/>\n</TAG>");
>
> I know the inverse function, DOMWriter.nodeToString, what's the right name for xxxFunction?
>
>Thank you,
> Simone.
It's not exactly a SOAP question, but I'm doing such things today,
so... in JAXP, that would be
String xmlStr="<TAG attr=\'abc\'>\n <CHILD/>\n</TAG>";
javax.xml.parsers.DocumentBuilderFactory dbf=
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); // and so forth, if relevant
dbf.setValidating(false);
javax.xml.parsers.DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(
new org.xml.sax.InputSource(
new java.io.StringReader(xmlStr)));
Element el=doc.getDocumentElement();
(and rather than DOMWriter.nodeToString you might consider using
a null JAXP transformer, as in
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(new DOMSource(el), new StreamResult(out));
where "out" is a java.io.Writer --- this is clipped from a jsp page;
but only if you want to maximize portability with the option of
running an XSLT stylesheet within the transformer later on...)
Now let's see if somebody posts something better. :-)
Tom Myers
Re: Element from String
Posted by Tom Myers <to...@dreamscape.com>.
At 07:02 PM 9/25/2001 +0200, Simone Badoer wrote:
>Hi.
>
>I need to obtain an Element from a String in a valid XML format, for example:
>
>Element el =xxxFunction("<TAG attr=\"abc\">\n <CHILD/>\n</TAG>");
>
> I know the inverse function, DOMWriter.nodeToString, what's the right name for xxxFunction?
>
>Thank you,
> Simone.
It's not exactly a SOAP question, but I'm doing such things today,
so... in JAXP, that would be
String xmlStr="<TAG attr=\'abc\'>\n <CHILD/>\n</TAG>";
javax.xml.parsers.DocumentBuilderFactory dbf=
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); // and so forth, if relevant
dbf.setValidating(false);
javax.xml.parsers.DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(
new org.xml.sax.InputSource(
new java.io.StringReader(xmlStr)));
Element el=doc.getDocumentElement();
(and rather than DOMWriter.nodeToString you might consider using
a null JAXP transformer, as in
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(new DOMSource(el), new StreamResult(out));
where "out" is a java.io.Writer --- this is clipped from a jsp page;
but only if you want to maximize portability with the option of
running an XSLT stylesheet within the transformer later on...)
Now let's see if somebody posts something better. :-)
Tom Myers