Help with Java and XML

Discussion in 'XML' started by Tom, Sep 8, 2005.

  1. Tom

    Tom Guest

    I need help to implement the following task in Java and any XML API but
    preferably JDOM. I am a total newbie to this.

    I have a DocBook document, for example:
    <chapter>
    <title>Title</title>
    <para>
    Text
    </para>
    <mediaobject>
    <imageobject>
    <imagedata fileref="image1.gif"/>
    </imageobject>
    </mediaobject>
    <sect1>
    <para>
    Text
    </para>
    <mediaobject>
    <imageobject>
    <imagedata fileref="image2.gif"/>
    </imageobject>
    </mediaobject>
    </sect1>
    </chapter>

    I want to find any <mediaobject> and prepend a comment containing the
    imagename, like
    <!-- image2 -->
    <mediaobject>
    <imageobject>
    <imagedata fileref="image2.gif"/>
    </imageobject>
    </mediaobject>

    The comment must not be prepended if it already exists! I have a
    problem to check if there is a comment before the <mediaobject>. I
    found a code to walk through all elements of the document with

    void walk( Element node, Collection allNodes )
    {
    // Add this node
    allNodes.add( node );
    // Recurse to add children
    Iterator i = node.getChildren().iterator();
    while( i.hasNext() )
    walk( (Element) i.next(), allNodes );
    }

    If I find a <mediaobject>, getParent() only returns another Element.
    But comments are no Elements, just Content.

    I don' know a way to search for all <mediaobjects> on a Content level.
    Maybe some other approach will be better but I have absolutly no clue.

    __
    Tom
     
    Tom, Sep 8, 2005
    #1
    1. Advertising

  2. Tom

    Sameer Guest

    Sameer, Sep 8, 2005
    #2
    1. Advertising

  3. On 8 Sep 2005 00:57:25 -0700, Tom wrote:

    > If I find a <mediaobject>, getParent() only returns another Element.
    > But comments are no Elements, just Content.


    I think that is the basic problem with your current approach.[1]

    You may need to transform the document before processing,
    or do a simpler search using String.indexOf to look for
    your comments.

    [1] I'll think some more and get back to this if I have any bright
    ideas, but thought that should be highlighted/stated now.

    [ Note: Follow-Ups to this post set to c.l.j.programmer only. ]

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "The captain seemed to understand, because the next day the cap' went out
    and drafted a band."
    The Andrews Sisters 'Boogie Woogie Bugle Boy'
     
    Andrew Thompson, Sep 8, 2005
    #3
  4. Tom wrote:
    > I need help to implement the following task in Java and any XML API but
    > preferably JDOM. I am a total newbie to this.
    >
    > I have a DocBook document, for example:
    > <chapter>
    > <title>Title</title>
    > <para>
    > Text
    > </para>
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image1.gif"/>
    > </imageobject>
    > </mediaobject>
    > <sect1>
    > <para>
    > Text
    > </para>
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image2.gif"/>
    > </imageobject>
    > </mediaobject>
    > </sect1>
    > </chapter>
    >
    > I want to find any <mediaobject> and prepend a comment containing the
    > imagename, like
    > <!-- image2 -->
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image2.gif"/>
    > </imageobject>
    > </mediaobject>
    >
    > The comment must not be prepended if it already exists! I have a
    > problem to check if there is a comment before the <mediaobject>. I
    > found a code to walk through all elements of the document with
    >
    > void walk( Element node, Collection allNodes )
    > {
    > // Add this node
    > allNodes.add( node );
    > // Recurse to add children
    > Iterator i = node.getChildren().iterator();
    > while( i.hasNext() )
    > walk( (Element) i.next(), allNodes );
    > }
    >
    > If I find a <mediaobject>, getParent() only returns another Element.
    > But comments are no Elements, just Content.
    >
    > I don' know a way to search for all <mediaobjects> on a Content level.
    > Maybe some other approach will be better but I have absolutly no clue.
    >
    > __
    > Tom
    >



    Use the getContent() method of the Element class like this:

    void walk( Element node, Collection allNodes )
    {
    // Add this node
    allNodes.add( node );
    // Recurse to add children
    Iterator i = node.getContent().iterator();
    while( i.hasNext() ) {
    org.jdom.Content content=(org.jdom.Content)i.next();
    if(content instanceof Comment) {
    // ok we have a comment
    Comment c=(Comment)content;
    }
    if(content instanceof Element) {
    walk( (Element) content, allNodes );
    }
    }
    }


    Hope this helps you,

    please look at the jdom api for more help.
    I got this answer from the api...
    so learn how to use a api.
     
    Tjerk Wolterink, Sep 8, 2005
    #4
  5. Tom wrote:
    > I need help to implement the following task in Java and any XML API but
    > preferably JDOM. I am a total newbie to this.
    >
    > I have a DocBook document, for example:
    > <chapter>
    > <title>Title</title>
    > <para>
    > Text
    > </para>
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image1.gif"/>
    > </imageobject>
    > </mediaobject>
    > <sect1>
    > <para>
    > Text
    > </para>
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image2.gif"/>
    > </imageobject>
    > </mediaobject>
    > </sect1>
    > </chapter>
    >
    > I want to find any <mediaobject> and prepend a comment containing the
    > imagename, like
    > <!-- image2 -->
    > <mediaobject>
    > <imageobject>
    > <imagedata fileref="image2.gif"/>
    > </imageobject>
    > </mediaobject>
    >
    > The comment must not be prepended if it already exists! I have a
    > problem to check if there is a comment before the <mediaobject>. I
    > found a code to walk through all elements of the document with
    >
    > void walk( Element node, Collection allNodes )
    > {
    > // Add this node
    > allNodes.add( node );
    > // Recurse to add children
    > Iterator i = node.getChildren().iterator();
    > while( i.hasNext() )
    > walk( (Element) i.next(), allNodes );
    > }
    >
    > If I find a <mediaobject>, getParent() only returns another Element.
    > But comments are no Elements, just Content.
    >
    > I don' know a way to search for all <mediaobjects> on a Content level.
    > Maybe some other approach will be better but I have absolutly no clue.
    >


    I haven't used JDOM, so this answer may confuse you because for some
    reason JDOM duplicates much of the DOM without actually being the DOM.
    (JDOM was written by smart people AFAIK, so there is probably a good
    reason.) So my comments apply to the standard DOM, i.e. the package
    org.w3c.dom which is bundled with the JDK.

    Everything in the XML document is a Node, even a comment. When you have
    a <mediaobject> node, you can use Node.getPreviousSibling() to next the
    node preceding it. If this Node is not a comment type (see
    Node.getNodeType()), then you need to add the comment.

    Note that if the document is not normalized (see Document.normalize()),
    you may get spurious text nodes between the comment and <mediaobject>
    element.

    HTH,
    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Sep 8, 2005
    #5
  6. On Thu, 08 Sep 2005 17:25:21 GMT, Raymond DeCampo wrote:

    > Everything in the XML document is a Node, even a comment.


    Huh, interesting! That doesn't gel with my (more limited)
    experience. I had not realised that any Java parsing method
    represented comments in the final, parsed structure.

    [ Follow-Ups to this post set to comp.lang.java.programmer only. ]

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "Give me a ticket for an aeroplane, ain't got time to take a fast train.."
    The Box Tops 'The Letter'
     
    Andrew Thompson, Sep 9, 2005
    #6
  7. Tom

    Tom Guest

    Ok, that is a good approach. I will work with that. Tank you.
     
    Tom, Sep 9, 2005
    #7
  8. Tom

    Tom Guest

    I think the code from Tjerk Wolterink gave me a hint in the right
    direction.
    Thank you all for your suggestions.
     
    Tom, Sep 9, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page