Skip navigation
rotoscopels
Currently Being Moderated

Remove Tag Contents In Dreamweaver?

Aug 25, 2012 10:02 PM

I have a XML file that has about 40 lines of code. Some of the tags have content inside and some are empty. I want to create a XML template out of this file for future use where I can easily insert the desired text into the empty fields.

 

I need a command in Dreamweaver that I can use line by line to remove the content inside each set of tags, but leaving the tags themselves intact (so I can insert text into in the future).

 

What command in Dreamweaver CS4 will easily clear these fields while preserving the tags themselves?

 
Replies
  • Currently Being Moderated
    Aug 26, 2012 1:57 AM   in reply to rotoscopels

    Without seeing an example of the XML file you plan to use, it's difficult to be sure. However, I believe the following should work:

     

    In Find and Replace, use the following settings:

     

    Find in: Current Document

    Search: Source Code

    Find:

    (<[^?>]+>)[^<]*(</[^>]+>)

    Replace:

    $1$2

     

    Make sure that the "Use regular expression" checkbox is selected.

     

    You can save this query for reuse by clicking the floppy disk icon at the top right of the Find and Replace dialog box.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 6:21 AM   in reply to rotoscopels

    rotoscopels wrote:

     

    Any easy way of "batch" removing these lines in Dreamweaver?

    Not that I know of.

     

    You could create a server-side script to do it for you. The following is a very crude example of how to do it in PHP:

     

    function editXML($file, $lineNumbers) {

        $source = file($file);

        $edited = array();

        for ($i = 0; $i < count($source); $i++) {

            if (in_array($i+1, $lineNumbers)) {

                continue;

            } else {

                $edited[] = $source[$i];

            }

        }

        $fh = fopen($file, 'w');

        fwrite($fh, implode('', $edited));

        fclose($fh);

    }

     

    editXML('test.xml', array(3,4));

     

    The editXML() function takes two arguments: the name of the file, and an array of line numbers to be removed. It draws each line of the file into an array, and then loops through the array omitting any line with the line number listed in the second argument. It finally writes the edited XML back to the original.

     

    It would probably be safer to write the edited files to a different directory, and you could automate it to process a whole directory at a time. But that should give you something to work on.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 8:37 AM   in reply to rotoscopels

    rotoscopels wrote:

     

    Is there a Find/Replace command I could use for multiple lines at once that would produce this effect:

     

    Line 4 <version></version>

    Line 21 <duration></duration>

    Line 23 <titleNum></titleNum>

    No.

     

    It would be a lot easier to help you if you showed an actual example of what it is you want to do rather than just give snippets of code.

     

    Dreamweaver supports regular expressions, which can perform complex find and replace operations. But usually the regex has to be designed for the individual situation. You can't strip content from tags on the basis of their line number.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 10:38 AM   in reply to rotoscopels

    Perhaps I'm missing something but why can't you create a dummy XML file without the values to use as a template? 

     

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 11:57 AM   in reply to rotoscopels

    rotoscopels wrote:

     

    Is the information your requesting not in the above post? You can clearly see the text in the tags I need removed is 5.0, 121 and 1. Is that not what your asking for?

    No, it's not. As I've said before, Dreamweaver cannot search and replace by line number.

     

    What it can do is search for patterns. If you want to remove the values from the  <version>, <duration>, and <titleNum> elements, you can do so with the following regular expression in the Find and Replace dialog box:

     

    Search: Source Code

    Find: (<(version|duration|titleNum)>)[^<]*(<\/\2>)

    Replace with: $1$3

     

    Make sure the "Use regular expression" check box is selected, and click Replace All.

     

    That will leave the tags, but remove the text from between them.

     

    If you want to remove the tags as well, just leave the "Replace with" field blank.

     

    However, and this is where it becomes difficult to provide the answer you need, the regular expression I have created won't remove the value from <movieReleaseYear>.

     

    Are you looking to remove all values or just some?

     

    Since it's XML, and you can predict the line numbers, you must be able to predict the names of the elements you want to clear. Or are the element names going to be different?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 10:56 AM   in reply to rotoscopels

    rotoscopels wrote:


    Let's say I use your (<(version|duration|titleNum)>)[^<]*(<\/\2>) command to clear the text from the <version>, <duration> and <titleNum> fields. What extra command would I have to add to "Search Source Code" to ALSO rename the <movieReleaseYear> text from 2007 to read 2010 in the same step?

    You can't do that in a single operation with the Find and Replace dialog box in Dreamweaver. You would need to run the first command to remove the values from the three fields. Then run a second command to replace the date.

     

    Find: <movieReleaseYear>\d{4}<\/movieReleaseYear>

    Replace with: <movieReleaseYear>2010</movieReleaseYear>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points