Fórum

Try to read xml-file in a MVCPortlet

Riccardo Berla, modificado 7 Anos atrás.

Try to read xml-file in a MVCPortlet

New Member Postagens: 17 Data de Entrada: 16/06/16 Postagens Recentes
I try to read to read an xml-file in a (Liferay 7.0.1 GA2) MVCPortlet like this:

@Override
	public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException {

		String XMLPATH = renderResponse.encodeURL(renderRequest.getContextPath() + "/resources/xml");

        try {
			ClassLoader classLoader = ReferenceCustomersPortlet.class.getClassLoader();
			InputStream xmlFile = classLoader.getResourceAsStream(XMLPATH + "/references.xml");

            JAXBContext jaxbcontext = JAXBContext.newInstance("biz.berla.myportal2.portlet.text.jaxb");
            Unmarshaller unmarshaller = jaxbcontext.createUnmarshaller();
			References references = (References) unmarshaller.unmarshal(xmlFile);
            List<references.reference> referenceList = references.getReference();

			// sort references in ascending order
			Collections.sort(referenceList, new ReferenceComparator());
            renderRequest.setAttribute("referenceList", referenceList);

        } catch (Exception e) {
            throw new PortletException(e);
        }

        super.render(renderRequest, renderResponse);

    }
</references.reference>

the XMLPATH + "/references.xml" turns to: /o/referencecustomersportlet/resources/xml/references.xml and the InputStream xmlFile stays empty. The structure of my portlet is like this:
reference-customers-portlet
src
...main
......java ...
......resources
.........content
.........META-INF.resources
............css
............img
............xml
...............references.xml
............init.jsp
............view.jsp

Maybe I am on a wrong way to solve this use-case. Reading the documentation I was tented to try the MVCResourceCommand to pass the xml-resource to my portlet. But I could not find a good example as guide to help me realize this solution. Any help is appreciated.
Riccardo Berla, modificado 7 Anos atrás.

RE: Try to read xml-file in a MVCPortlet

New Member Postagens: 17 Data de Entrada: 16/06/16 Postagens Recentes
I resolved my problem like this:

I set my XMLPATH to: String XMLPATH = "META-INF/resources/xml";
and my inputStream to:
InputStream inputStream = ReferenceCustomersPortlet.class.getClassLoader().getResourceAsStream(XMLPATH + "/references.xml");

and unmarshal my inputStream:
References references = (References) unmarshaller.unmarshal(inputStream);

Thanks for reading and commenting.
Riccardo