tags:

views:

165

answers:

1

Hello, could anybody tell me how to parse XML file inside a JSF page?

The thing's that I've got XML file and want to put some data from it into my JSF page. My first thought was to include JSTL Core and XML libs and to do something like this:

<c:import var="some-info-doc" src="some-info.xml" /> 
<x:parse  var="some-info-xml" xml="some-info-doc" />

<h:outputText>       
   <x:out select="$some-info-xml/a-piece-of-data" />         
</h:outputText>

However, this code resulted in error. c:import was not recognized. So, I decided to play with a local piece of XML:

<x:parse var="simple">
   <child>basic</child>
</x:parse>

<h:outputText>       
   <x:out select="$simple/child" />      
</h:outputText>

This led to the child tag being printed in the result page. And the output came from the x:parse tag, not h:output.

So that, is there any alternative to parse XML inside a JSF page (not including XML being sent as an object from a certain by-me-written module)? Or are there any errors in my code?

+1  A: 

1. Don't try to mix JSTL tags and JSF tags; they're chalk and cheese.

2. JSF is an MVP framework, so you're going against the grain by trying to define your data sources in the view.

3. To emit data via an outputText control, bind its value attribute to the model (e.g. a managed bean).

It is probably possible to do something like this:

<!-- other code elided -->
<x:set var="x" select="$simple/child" />
<h:outputText value="#{x}" />

...but, in general, see points 1 and 2.


Just a suggestion: ensure you've added the http://java.sun.com/jsp/jstl/core namespace to the page to use JSTL core.

McDowell
1. I'll try hard. 2. Actually, I wanted to define the structure of my pages via XML. I just like the level of abstraction it offers. I've posted a question, which should cover this theme: http://stackoverflow.com/questions/1641886/xml-as-structure-jsf-as-representation-is-it-a-nice-idea. 3. Unfortunately, this doesn't work. Suppose, I should forget about XPath while working with JSF. I will miss it
emanemos
A comment on your comment, emanemos: you do **not** need to *forget* XPath. You just need to *move* the desired logic to the right place, e.g. a servlet or a managed bean.
BalusC