On 05/30/2011 05:05 PM, Roderik Dernison wrote:
>  
> 
> I got a lot working from java, but now I’m stuck setting dita elements
> like author. I managed to set the title from java.
> 
> What’s the difference between setting the title and the author?
> 
>  
> 
> This is the code I use:
> 
>  
> 
>             Name author = Name./get/("author");
> 
>             Element authorElement = *new* Element(author);
> 
>             authorElement.putAttribute(Name./get/("type"), "creator");
> 
>             String username = System./getProperty/("user.name");
> 
>             authorElement.setText(username);
> 
> This is the result of adding the element:
> 
>     <author type="creator"/>
> 
>  

Convenience method Element.setText does not create text nodes. It merely
replaces some text in an existing child text node. A newly created
element has no child nodes whatsoever, that's why, in the above case,
setText does nothing at all.

Please replace:

---
authorElement.setText(username);
---

by:

---
authorElement.appendChild(new Text(username));
---

References:

*
http://www.xmlmind.com/xmleditor/_distrib/doc/api/com/xmlmind/xml/doc/Element.html#setText(java.lang.String)

*
http://www.xmlmind.com/xmleditor/_distrib/doc/api/com/xmlmind/xml/doc/Tree.html#appendChild(com.xmlmind.xml.doc.Node)

*
http://www.xmlmind.com/xmleditor/_distrib/doc/api/com/xmlmind/xml/doc/Text.html#Text(java.lang.String)



---
PS:

Method Element.setText is implemented as follows:
---
public boolean setText(String text) {
    // Check it can be done ---

    Node node = first;
    while (node != null) {
        if (node.getType() == Type.ELEMENT) {
            // setText fails if this element contains child elements.
            return false;
        }
        node = node.next;
    }

    // Do it ---

    boolean done = false;

    node = first;
    while (node != null) {
        Node next = node.next;

        if (node.getType() == Type.TEXT) {
            Text textNode = (Text) node;

            if (!done) {
                done = true;
                if (document != null) {
                    document.beginEdit();
                }

                textNode.setText(text);
            } else {
                // This happens when comments and/or processing
                // instructions have been inserted in the middle of Text
                // nodes.
                removeChild(textNode);
            }
        }

        node = next;
    }

    if (done && document != null) {
        document.endEdit();
    }

    return done;
}
---

If you have access to the full source code of XMLmind XML Editor, the
quickest way to get unstuck is to take a look at this source code.
You'll see that this source code is pretty easy to follow.



 
--
XMLmind XML Editor Support List
[email protected]
http://www.xmlmind.com/mailman/listinfo/xmleditor-support

Reply via email to