Re: Don't understand NodeModel.get with XML

"Ista Pouss" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
It's work !

If I do :

        String txt = "<root><a/><b/></root>";
        NodeModel mod = NodeModel.parse(
                new InputSource(new StringReader(txt)));
        System.out.println("hope it's doc ? = "+mod);
        TemplateSequenceModel seqroot = mod.getChildNodes();
        NodeModel root = (NodeModel)seqroot.get(0);
        TemplateSequenceModel childsroot = root.getChildNodes();
        NodeModel hopea = (NodeModel) childsroot.get(0);
        NodeModel hopeb = (NodeModel) childsroot.get(1);
        System.out.printf("hope it's root ? = %s - %s%n", root,
root.getNodeName());
        System.out.printf("hope it's a ? = %s - %s%n", hopea,
hopea.getNodeName());
        System.out.printf("hope it's b ? = %s - %s%n", hopeb,
hopeb.getNodeName());

I see on console :

hope it's doc ? = freemarker.ext.dom.DocumentModel@108786b
hope it's root ? = freemarker.ext.dom.ElementModel@1ad086a - root
hope it's a ? = freemarker.ext.dom.ElementModel@10385c1 - a
hope it's b ? = freemarker.ext.dom.ElementModel@42719c - b

It's OK and, more difficult, I understand.


Why this stupid code ? It's for debugging purpose.

I have a TemplateMethodModel which return a NodeModel from a XML String and,
sometimes the XML attributes disapear , I don't understand why.

<#macro elemName>
<#assign ln = .node.@att> <!-- sometimes no attributes here, but there are
in the XML String -->

// from the TemplateMethodModel :
    public Object exec(List list) throws TemplateModelException
    {
      NodeModel mod;

      mod = null;
      try
      {
        String texte;

        texte = (String)list.get(0);
        if (!texte.startsWith("<?xml"))
          texte =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+
                "<txtDescriptif>"+texte+"</txtDescriptif>";
        //
        mod = NodeModel.parse(new InputSource(new StringReader(texte)));
      }
      catch (Exception excp)
      {
        ...
      }
      return mod;
    }


I explore... I don't understand... if you have any suggestion...





2008/5/5 Stephan Müller <[email protected]>:

> Ista Pouss schrieb:
> > Hi,
> >
> > If I do that :
> >
> >        String txt = "<root><a/><b/></root>";
> >         NodeModel mod = NodeModel.parse(
> >                 new InputSource(new StringReader(txt)));
> >         System.out.println("hope it's root ? = "+mod);
> >         System.out.println("hope it's a ? = "+mod.get(0));
> >         System.out.print("hope it's b ? = "+mod.get(1));
> >
> >
> > I see on console :
> >
> > hope it's root ? = freemarker.ext.dom.DocumentModel@108786b
> > hope it's a ? = freemarker.ext.dom.DocumentModel@108786b
> > hope it's b ? = null
> >
>
> First of all, I don't see why you try to navigate through the wrapped
> DOM document like this, since the main (and only) intended use of the
> NodeModel API is from inside a FreeMarker template. Nevertheless it is
> possible to achieve this with the NodeModel API. One problem is that the
> result of NodeModel.parse() returns the document node, not the
> document's root node. Secondly, AFAIK, you cannot use the
> NodeModel.get(int) methods to access the child nodes. You have to use
> NodeModel.getChildNode().get(int) instead:
>
> String txt = "<root><a/><b/></root>";
> NodeModel documentNode = NodeModel.parse(
>     new InputSource(new StringReader(txt)));
>
> NodeModel rootNode = (NodeModel)documentNode.getChildNodes().get(0);
> System.out.println("hope it's root ? = "+rootNode.getNodeName());
>
> NodeModel nodeA = (NodeModel)rootNode.getChildNodes().get(0);
> System.out.println("hope it's a ? = "+nodeA.getNodeName());
>
> NodeModel nodeB = (NodeModel)rootNode.getChildNodes().get(1);
> System.out.print("hope it's b ? = "+nodeB.getNodeName());
>
> Alternatively you can use XPath expressions to navigate through the
> document:
>
> ArrayList xpath = new ArrayList();
> xpath.add("/root/a");
> NodeModel anotherNodeA = (NodeModel)documentNode.exec(xpath);
> System.out.println("hope it's a ? = "+anotherNodeA.getNodeName());
>
>
>
>
> HTH,
> Stephan.
>

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.