Hi Ivano,
Another option would be to do:
Document doc = resource.get(Document.class);
That should just works with automagic conversion :)
Best regards,
Jerome
--
Restlet ~ Founder and Technical Lead ~ http://www.restlet.org
Noelios Technologies ~ http://www.noelios.com
-----Message d'origine-----
De : [email protected] [mailto:[email protected]] De la part de Alex
Milowski
Envoyé : jeudi 25 août 2011 20:26
À : [email protected]
Objet : Re: Version 2: How to get XML representation
On Thu, Aug 25, 2011 at 10:10 AM, Ivano Carrara <[email protected]> wrote:
> Hi all,
>
> Using Version 1 of Restlet, I built a client to get Xml from a remote server
> and to examine it using Xpath expressions - below a fragment of code:
>
> Request req = new Request(Method.GET, url); Client client = new
> Client(Protocol.HTTP); Response resp = client.handle(req);
>
> DomRepresentation doc = resp.getEntityAsDom();
>
> How I can obtain the same "doc" object using the Version 2 ?
>
> Below the actual code:
>
> ClientResource resource = new ClientResource(url); resource.get();
>
> Then using "resource.get().getText());" I obtain the text of my XML, but I
> need the DOM representation to use with XPath expressions.
>
> With Version 2, what is the equivalent code to "DomRepresentation doc =
> resp.getEntityAsDom();" of Version 1 ?
You can either use the DocumentBuilder API from JAXP:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(new
InputSource(resource.get().getReader()));
or you can use the DomRepresentation from the XML extension:
DomRepresentation domRep = new DomRepresentation(resource.get());
Document doc = domRep.getDocument();
I personally use the DocumentBuilder API so I have control over the DOM
construction.
--Alex Milowski
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2829756
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2830472