Results 1 to 4 of 4
  1. #1
    Fliz is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Sax XML parse - printing outer not inner nodes

    Hi, I am parsing XML with Java using SAX. I want to print the XML, but only to print the outer nodes not any inner nodes. What is the best way to approach this within the start element handler?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fliz View Post
    Hi, I am parsing XML with Java using SAX. I want to print the XML, but only to print the outer nodes not any inner nodes. What is the best way to approach this within the start element handler?

    Thanks
    Supply the parser with your own DefaultHandler; the parser will call your startElement method when it enters a node and call endElement when it leaves a node. Keep track of a counter and increment it when entering a node and decrement it when the parse leaves a node. Only print the node content when the counter == 1.

    kind regards,

    Jos

  3. #3
    Fliz is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    hi, thanks I have achieved this now but I have an xml file with many of one 'item' with many inner nodes. It only seems to count the first 'item' so will only print from one 'item' not all. Am I doing something wrong

    e.g.

    <Items>
    <Item>
    <example node></example node>
    <example node></example node>
    <example node></example node>
    </Item>
    <Item>
    <example node></example node>
    <example node></example node>
    <example node></example node>
    </Item>
    </Items>

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Can you show us some of your (relevant) code? (keep it as short as possible please).

    kind regards,

    Jos

Similar Threads

  1. Replies: 0
    Last Post: 07-15-2009, 01:46 PM
  2. How to dynamically add Nodes to JTree?
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 07-13-2009, 07:20 AM
  3. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 04:22 PM
  4. nodes in java netowork
    By ahsan in forum Advanced Java
    Replies: 0
    Last Post: 12-26-2007, 03:11 PM
  5. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 03:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •