cancel
Showing results for 
Search instead for 
Did you mean: 

XML string parsing issue

svast
Champ on-the-rise
Champ on-the-rise
Hi there : I have lots of trouble when I want to parse XML in Webscript (in Javascript or FTL, i can't get it to work).
The XML is stored as a property of a alfresco node.

I get the XML  with Javascript:
var xmlstring = node.properties["ph:ARActeXml"].content;

I can pass it to the Freemarker template to verify it is XML string
model.myxmlstring = xmlstring

So I see I could use that XML string in either Javascript and/or Freemarker engine. I am just lacking the API.
But how can I parse it to retrieve some property inside it?
I am running Alfresco Community 2.1, and it looks like the RHINO xmlNodeModel, new XML,… have not been included in that version.

Please any kind of help will be apreciated; i looked at the tutorials but the methods all failed  Smiley Sad
10 REPLIES 10

tommorris
Champ in-the-making
Champ in-the-making
XML parsing has been enabled in later versions of Alfresco (2.2 and 2.9…I think…).

But it's easy to patch to enable E4X support.
I followed this http://issues.alfresco.com/browse/AR-1720 with success.
After doing this, parsing XML suddenly becomes super easy.

Also read this, if you're interested: http://forums.alfresco.com/viewtopic.php?f=27&t=9479&p=37232

Tom

svast
Champ on-the-rise
Champ on-the-rise
Hi,

your first URLhttp://issues.alfresco.com/browse/AR-1720  is a very good start point. It did work for me, although I had to heavily fight against namespaces handling to make it work as expected.
Hint for EAX beginners: there is a good FAQ here that solved most of my problems, especially regarding namespace:
http://www.faqts.com/knowledge_base/index.phtml/fid/1762

The 2nd URL is more elegant from my point of view, but it did not work for me (is Alfresco tweaking Rhino before import?).

What i did is importing xbean.jar and jsr173_1.0_api.jar from the binary package of the XMLBeans binary distribution (from http://xmlbeans.apache.org/ ) into my project instead of the suggested ${TOMCAT_HOME}/shared/lib directory.
So I have it in my AMP package, and it deploys in alfresco/tomcat/webapps/alfresco/WEB-INF/lib

It works perfectly.
Thanks to all the community!

mikeh
Star Contributor
Star Contributor
Freemarker also natively supports XML parsing. See http://freemarker.sourceforge.net/docs/xgui.html for details.

Thanks,
Mike

svast
Champ on-the-rise
Champ on-the-rise
Yes, I've read that also, but could not work with it (maybe because of namespace issues …).

It is worth having a look at it again, but unfortunately there is no clear piece of tutorial to perform what I want: retrieve a property from a XML string.
Example: I have a string named "myxmlstring" containing:
<?xml version="1.0" encoding="ISO-8859-1"?>
<machin:Amachin xmlns:machin="http://www.mycompany.net/MACHIN#v1.1-20040216" xmlns:insee="http://xml.insee.fr/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mycompany.net/MACHIN#v1.2-20071201 machinv1_2.xsd" machin:DateReception="2008-03-21" machin:ID="000-123456789-20080321-200803211-91">
  <machin:Recu machin:Date="2008-03-21" machin:Num="200803211" machin:Code="3">
    <machin:CodeMatiere1 machin:CodeMatiere="2" />
    <machin:CodeMatiere2 machin:CodeMatiere="2" />
    <machin:Document>
      <machin:NomPDF>000-123456789-20080321-200803211-91.pdf</machin:NomPDF>
    </machin:Document>
  </machin:Recu>
  <machin:Version>2007-12-12</machin:Version>
</machin:Amachin>
What is the Freemarker code to extract the machinSmiley Very HappyateReception property containing the value "2008-03-21" ?

mikeh
Star Contributor
Star Contributor
Did you see the section on namesspaces here? http://freemarker.sourceforge.net/docs/xgui_imperative_learn.html

It might also be worth taking the question over to the Freemarker community https://sourceforge.net/forum/?group_id=794

Thanks,
Mike

jamesc5
Champ in-the-making
Champ in-the-making
Mike,

Would it be possible for you to elaborate or post a sample? I'm having issues with the same thing. In my .js file I have

model.file = store.lookupNode("ROOT/press-on.xml")

In my freemaker template I have

<#ftl ns_prefixes={"D":"http://www.alfresco.org/alfresco/pr"}>

<#assign release = file.xmlNodeModel>

${release?node_type}<br>
${(release?node_namespace??)?string}<br>
#{release.children?size}

Which outputs

document
false
0

I know that there is content in my node because if I change my js file to

model.file = store.lookupNode("ROOT/press-on.xml").content

model.file contains a string with the expected content.

this is press-on.xml

<pr:press_release>
<pr:title>Keep on Pressin On</pr:title>
<pr:abstract>I can not be implemented</pr:abstract>
<pr:location>San Francisco, CA</pr:location>
−
   <pr:body>
<div id="lipsum"> <p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tincidunt metus non risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed congue. Nullam luctus. Mauris vulputate erat at mauris. Duis commodo sem sit amet purus. Suspendisse id mauris. Vivamus laoreet metus ut justo. Curabitur nec odio. Suspendisse volutpat leo vitae odio. </p> <p>Vivamus purus risus, suscipit ut, gravida ac, egestas id, tortor. Mauris ut odio ac magna cursus sagittis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris et magna eu purus vulputate sodales. Proin ultrices elit et lorem. Curabitur pretium erat vitae ligula. Praesent porta dignissim enim. Nulla quam. Praesent consectetuer est rhoncus nunc. Ut elementum condimentum est. Nam ullamcorper. Suspendisse in dolor non enim scelerisque commodo. Aliquam vitae pede consequat dolor rutrum pharetra. Vestibulum ut sem et nulla sodales consectetuer. Proin dictum. Curabitur eleifend. Mauris non nulla. Nulla dictum viverra arcu. Aenean sem. Aenean et dolor. </p></div>
</pr:body>
<pr:include_media_contacts>true</pr:include_media_contacts>
<pr:launch_date>2008-05-20</pr:launch_date>
<pr:expiration_date>2008-07-17</pr:expiration_date>
</pr:press_release>

jamesc5
Champ in-the-making
Champ in-the-making
I figured out how to get output   Smiley Very Happy

I'm still confused about why the "child" property of the document node is returning 0, but here's code that works for the XML I posted below.


<#ftl ns_prefixes={"D":"http://www.alfresco.org/alfresco/pr"}>

<#if file.mimetype = "text/xml">
   <#assign dom=file.xmlNodeModel>
   <press_release>
      <title>${dom.press_release.title}</title>
      <description>${dom.press_release.abstract}</description>
   </press_release>
</#if>

martin_cowie
Champ in-the-making
Champ in-the-making
Long term, I'd go for the baked in E4X support, however if that doesn't work "XML For Script" (http://xmljs.sourceforge.net/) is something that I'm planning on using to solve a similar similar. It might work for you too.

M.

stebans
Champ in-the-making
Champ in-the-making
Hi,

using Alf-2.1CE, you can upgrade Rhino engine to 1.6R7 ou 1.7R1 for having E4X native support without any dependancies on Xmlbeans.
(just replace js rhino engine in lib folder). In case of using 1.7R1, check this address for compiling the debugger http://developer.mozilla.org/en/docs/New_in_Rhino_1.7R1
see  Paragraph "Debugger must be built after download" which seems to be required in Alfresco.

Beware of the bug about xml declaration (http://developer.mozilla.org/en/docs/E4X)
One thing to note is the toXMLString() method for getting the root:
x = <a>some stuff</a>
logger.log(x)
-> some stuff ((typeof x -> xml)
logger.log(x.toXMLString())
-><a>some stuff</a>

Thanks about the info about freemarker and XML :wink:
Regards
Stéphane