[opencms-dev] (no subject) - retrieve xml content manually

Peter Birchmeier peterbirchmeier at hotmail.com
Tue Sep 27 09:58:31 CEST 2005


Hi Bernd, hi all,

thank you for the info, it's exactly what I wanted to hear :)
Yes you're right, i have sometimes the impression Java is slow like a snail, 
expacially if xml has to be processed, but it isn't of course.

Concerning my code: it was just an unstructered example of how to retrieve 
the content. It had some mistakes and the fact that the document is 
unmarshalled only once wasn't obvious. So i will repost the code at the end.
I think the OpenCms' caching mechanism is indead a good point. When I looked 
through the code, i remarked several method calls to the FlexCache module, 
which I simply ignored in my code.

But finally, I like the proposition of using jstl, it's smart and uses the 
content show methods of the tag classes.

kind regards, Peter


Here the code again, a mixture of jsp-directives and -scriptlets, but i'm 
convinced that there better solutions... however:

<%@ page import=".." %>

<%
// before the xml resource loop starts
CmsJspActionElement cms = ..
CmsObject obj = cms.getCmsObject(); // no flex cache
List collectorResult = collectXmlResources( "allInFolder..", 
"article_${number}|article|4" );
%>

<%
// for each xml resource
CmsResource resource = (CmsResource) collectorResult.get( 0..n-1 );
I_CmsXmlDocument xml = getXmlDocument( resource, obj );

// single elements, locale has to be found out first
String content = xml.getStringValue( "ElementName", locale );

%><%= content %>

<%!
public List collectXmlResources( String collectorName, String 
collectorParam, CmsObject obj ) {
  I_CmsResourceCollector collector =
    OpenCms.getResourceManager().getContentCollector( collectorName );
  List collectorResult = collector.getResults( obj, collectorName, 
collectorParam);
  return collectorResult;
}

public I_CmsXmlDocument getXmlDocument( CmsResource resource, CmsObject obj 
) {
  CmsFile file = CmsFile.upgrade( resource, obj );
  I_CmsXmlDocument content = CmsXmlContentFactory.unmarshal( obj, file );
  return content;
}
%>

>From: Bernd Wolfsegger <bw at code-create.com>
>Reply-To: Bernd Wolfsegger <bw at code-create.com>,The OpenCms mailing list 
><opencms-dev at opencms.org>
>To: The OpenCms mailing list <opencms-dev at opencms.org>
>Subject: Re: [opencms-dev] (no subject) - retrieve xml content manually
>Date: Tue, 27 Sep 2005 08:25:36 +0300
>
>I think the amount of code and its effect on performance is highly
>overestimated.
>The source code will be compiled in very efficient byte code, depending on 
>the
>style of coding.
>So it's more a question of the coding style than the amount of source code.
>
>What I do not understand in your code is why you have a instance variable
>"resource" and also a local variable "resource " in your getContent() 
>method.
>You never use the "instance" variable.
>
>Or is the first part meant to be in an JSP?
>
>In the getContent() method below you unmarshal the content nevertheless 
>each
>time you request an element.
>
>If you use the OpenCms caching mechanism in a clever way, most of the time 
>the
>content will come from cache and no unmarshalling takes place at all.
>It's seldom the case, that content changes form one minute to another, so 
>it's
>not necessary to retrieve it for each request anew from the database.
>
>There is a general Problem, that the whole xmlcontent of a resource is kept 
>in
>one database field. So you have to use Java to retrieve the value of single
>elements and not SQL.
>But in the past I have noticed that it is much faster, lets say, to 
>retrieve
>10000 Datarows form a table in one suck and then get the one you want by
>using Java code, than to search for this row using SQL and accessing the
>database.
>
>Java IS fast.
>
>But may be my SQL code was not very efficient :)
>
>You can use JSTL also to get hands on content, still using cms tags (This 
>is
>just an example, based on nested xsd!):
>
><cms:contentcheck ifexists="Detail">
>	<cms:contentloop element="Detail" >
>		<cms:contentcheck ifexists="ImageBox">
>			<c:set var="thumbImgWidth">
>				<cms:contentshow element="ImageBox/ThumbImageWidth" />
>			</c:set>
><%
>String value = (String) pageContext.getAttribute("thumbImgWidth");
>// Do something with your knowledge ...
>%>
>		</cms:contentcheck>
>	</cms:contentloop>
></cms:contentcheck>
>
>Kind regards, Bernd
>
>On Tuesday, 27. September 2005 01:12, Peter Birchmeier wrote:
> > Thanx, i didn't know that method. But after looking through its source
> > code, i think its rather a slow way to retrieve xml content since the 
>xml
> > file must be unmarshalled for eaxh element...
> >
> > I tried to find a way with as less code as possible, which is still fast
> > enough.
> >
> >
> > something like that might do the job:
> >
> > import org.opencms.jsp.*, org.opencms.main.*, import org.opencms.file.*,
> > org.opencms.file.collectors.*, import org.opencms.xml.*,
> > org.opencms.xml.content.*;
> >
> > CmsJspActionElement cms = ..
> >
> > I_CmsResourceCollector collector =
> > OpenCms.getResourceManager().getContentCollector(m_collectorName);
> > List collectorResult = collector.getResults(cms.getObject(),
> > m_collectorName, m_collectorParam);
> >
> > CmsResource resource = (CmsResource) collectorResult.get( 0..n );
> >
> > public String getContent( CmsResource resource, String element, Locale
> > locale) {
> > 	CmsFile file = CmsFile.upgrade( resource, cms.getCmsObject() );
> > 	I_CmsXmlDocument content = CmsXmlContentFactory.unmarshal(
> > cms.getCmsObject(), file );
> >
> > 	Locale loc = OpenCms.getLocaleManager().getBestMatchingLocale( locale,
> > 		OpenCms.getLocaleManager().getDefaultLocales(cms,
> > cms.getSitePath(resource)),
> > 		content.getLocales(element));
> >
> > 	return content.getStringValue( element, loc );
> > }
> >
> > hmmm.. i'm not sure if it is a good idea to use too much code..
> > maybe anybody has a better idea? The most practical way would be if
> > xmlcontentload-tags could be used together with scriptlets that request 
>a
> > specific xml content element from the loaded xml file...
> >
> > >From: Bernd Wolfsegger <bw at code-create.com>
> > >Reply-To: Bernd Wolfsegger <bw at code-create.com>,The OpenCms mailing 
>list
> > ><opencms-dev at opencms.org>
> > >To: The OpenCms mailing list <opencms-dev at opencms.org>
> > >Subject: Re: [opencms-dev] (no subject)
> > >Date: Mon, 26 Sep 2005 16:19:58 +0300
> > >
> > >Hi Peter,
> > >
> > >simply use "cms.getContent(String resourcePath, String elementName, 
>Locale
> > >locale)"
> > >
> > >Kind Regards, Bernd Wolfsegger
> > >
> > >On Monday, 26. September 2005 16:02, Peter Birchmeier wrote:
> > > > Hi,
> > > >
> > > > I do a xml files load and query single xml elements by
> > >
> > ><cms:contentshow..
> > >
> > > > I'ld like to be able to have more options with the output, e.g. 
>check a
> > > > certain element whether it is empty or not (empty String "", not 
>null).
> > > >
> > > > I couldn't find any way with the given standard xml tags. Have I
> > >
> > >overseen
> > >
> > > > anything?
> > > >
> > > > So my idea was to manually query the elements by jsp scriptlets. But 
>as
> > >
> > >far
> > >
> > > > as I have seen in the CmsJspContentShowTag source code I would have 
>to
> > > > implement several steps to achieve the data. Therefore it might be
> > >
> > >easier
> > >
> > > > to do the whole xml content load manually, by calling the 
>responsible
> > > > Collector class....
> > > >
> > > > Is there a better way?
> > > > thanks for your hints,
> > > > kind regards, Peter
> > > >
> > > > _________________________________________________________________
> > > > Behalten Sie Ihre Hotmails, den Messenger und die Suchfunktionen 
>stets
> > >
> > >im
> > >
> > > > Auge! http://toolbar.msn.ch?&DI=165&XAPID=2170 Jetzt downloaden!
> > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > This mail is send to you from the opencms-dev mailing list
> > > > To change your list options, or to unsubscribe from the list, please
> > >
> > >visit
> > >
> > > > http://mail.opencms.org/mailman/listinfo/opencms-dev
> > >
> > >--
> > >
> > >[  Code Create
> > >[  Web Content Management and Presentation
> > >
> > >
> > >[  Bernd Wolfsegger
> > >[  Sun Certified Programmer for Java(TM) 2 Platform
> > >
> > >
> > >[  Lohmeyerstrasse 13
> > >[  10587 Berlin
> > >[  Germany
> > >[  Fon +49 (0)30 26555788
> > >[  Fax +49 (0)30 2651835
> > >[  Mobile +49 (0)163 6505622
> > >
> > >[  bw at code-create.com
> > >[  http://www.code-create.com/
> > >
> > >
> > >
> > >_______________________________________________
> > >This mail is send to you from the opencms-dev mailing list
> > >To change your list options, or to unsubscribe from the list, please 
>visit
> > >http://mail.opencms.org/mailman/listinfo/opencms-dev
> >
> > _________________________________________________________________
> > Essenzielle Infos von A-Z! Starten Sie eine Online-Recherche mit MSN
> > Search! http://search.msn.ch/ Jetzt neu! Jetzt gratis downloaden!
> >
> >
> >
> > _______________________________________________
> > This mail is send to you from the opencms-dev mailing list
> > To change your list options, or to unsubscribe from the list, please 
>visit
> > http://mail.opencms.org/mailman/listinfo/opencms-dev
>
>--
>
>[  Code Create
>[  Web Content Management and Presentation
>
>
>[  Bernd Wolfsegger
>[  Sun Certified Programmer for Java(TM) 2 Platform
>
>
>[  Lohmeyerstrasse 13
>[  10587 Berlin
>[  Germany
>[  Fon +49 (0)30 26555788
>[  Fax +49 (0)30 2651835
>[  Mobile +49 (0)163 6505622
>
>[  bw at code-create.com
>[  http://www.code-create.com/
>
>
>
>_______________________________________________
>This mail is send to you from the opencms-dev mailing list
>To change your list options, or to unsubscribe from the list, please visit
>http://mail.opencms.org/mailman/listinfo/opencms-dev

_________________________________________________________________
So können Sie zu jeder Zeit die besten Suchfunktionen nutzen - die MSN 
Toolbar. http://toolbar.msn.ch?&DI=165&XAPID=2170




More information about the opencms-dev mailing list