Results 1 to 5 of 5
  1. #1
    Mitch_H is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Question How do I skeletonize XML using the DOM object?

    I have thousands of variations of the ubber-simplified XML listed below which I must "Skeletonize" and create an output version of the new transformed xml. (Spaces replaced with dots to maintain indentation for readability)

    Java Code:
    </sampleDoc>
      <stateCounty>
        <state abbreviation="CA" fips="06">
          <name>California</name>
        </state>
        <county code="RV" fips="065">
          <name>Riverside</name>
        </county>
      </stateCounty>
      <buildInfo>
        <date>20120529</date>
        <time>11.08.00</time>
      </buildInfo>
      <userInfo>
        <userId>
          <id securityLevel="23">12345678</id>
          <name>Fred Flintstone</name>
        </userId>
      </userInfo>
    </sampleDoc>
    I need to remove some of the elements, blank out others, and blank out some attributes, to create the resulting skeleton XML which still has the <stateCounty> element and all it's children, deleted the <buildInfo> element and children, and removed the values from the attributes and element data in <userInfo>'s children as shown below.

    Java Code:
    </sampleDoc>
      <stateCounty>
          <state abbreviation="CA" fips="06">
          <name>California</name>
        </state>
        <county code="RV" fips="065">
          <name>Riverside</name>
        </county>
      </stateCounty>
      <userInfo>
        <userId>
          <id securityLevel=""></id>
          <name></name>
        </userId>
      </userInfo>
    </sampleDoc>
    I am traditionally a mainframe programmer and am fairly new to Java. I have been searching for information on how to use the DOM object, but don't understand how to determine the difference between the /sampleDoc/stateCounty/state/name and /sampleDoc/userInfo/userId/name elements? They both seem to have the same element name "name".

    If anyone can provide any insight on how to process this tree, your assistance would be greatly appreciated.

    Mitch Harper
    Last edited by Fubarable; 05-31-2012 at 11:45 PM. Reason: code tags added

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: How do I skeletonize XML using the DOM object?

    If you have anyone with xslt (ie xml transformation) knowledge then I would simply transform the former into the latter.
    It's a pretty simple one (assuming that is the actual data format).

    If not, and you need to do this via Java, then you might want to use a SAX parser, which will let you trace down the tree.
    Failing that (and since you seem to be looking at DOM) you could use XPath.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: How do I skeletonize XML using the DOM object?

    Because I'm a lazy old sod I'd use a SAXParser and use an extension of a DefaultHandler object; that DefaultHandler is used as a callback function when the SAXParser parses the XML text. No fat DOM model is created and you can create your skeleton output while the input is being parsed; all lean and mean ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: How do I skeletonize XML using the DOM object?

    I'd transform it myself.
    But then I did shed loads of that a few years back, so it's almost second nature.

    The DOM is a problem, especially if the source is big.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Mitch_H is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: How do I skeletonize XML using the DOM object?

    Tolls and JosAH,

    Thanks for your advise. I was able to tackle the beast using the SAX Parser as recommended.

    Your advise is much appreciated.

    Mitch Harper

Similar Threads

  1. Replies: 1
    Last Post: 01-09-2012, 05:46 PM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  4. Replies: 2
    Last Post: 01-29-2010, 03:01 AM
  5. Replies: 1
    Last Post: 03-04-2009, 07:14 PM

Posting Permissions

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