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?
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.
Thanks for the info! However, it would seem there is a slight change of plan. Perhaps you could help again.
I've decided that the easiest way to maintain the large collections of XML files is to just let the program I'm using automatically create them as needed and populate all the fields with the code/tags. Then, instead of trying to copy and paste just the info I need over to my blank XML template, I could just remove the lines from original XML file and save it when I'm done. Then just do that for each entry. I've already verified the lines I need to remove do not affect functionality. It's just extra data that's bloating the file I'll need to remove it each time.
Here's the thing. The XML file that is generate has the same fields and the same number of lines in each one. So, if the code I need to remove always fall on the same lines, could I use a similar command as above to easily remove these lines with a simple click?
For example, the current XML file I'm editing has 36 Lines with the data that I don't need falling on the following lines:
4,6,7,11,12,16,17,18,19,22,25 and 26.
Any easy way of "batch" removing these lines in Dreamweaver?
Thanks!
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.
Ah, I see. Perhaps we could take another approach.
Instead of removing these lines all together, what if I just left the tags on these lines and removed the text written inside? I don't care if I'm left with several lines of empty tags (as I do believe there is a command to remove all those later).
For example:
Line 4 <version>5.0</version>
Line 21 <duration>121</duration>
Line 23 <titleNum>1</titleNum>
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>
Thanks!
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.
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?
The original code is here:
<version>5.0</version>
<studio>
</studio>
<language>
</language>
<movieReleaseYear>2007</movieReleaseYear>
<largeCoverParams>
</largeCoverParams>
<smallCoverParams>
</smallCoverParams>
<duration>121</duration>
<title>
<titleNum>1</titleNum>
Weather it be by line or by tag set, the text will always be different so I can't search by that. However, I can search by the tags that appear on a specific line number as they WILL always be generated on the same line (IE: <version></version> always gets generated on line 4, <duration></duration> always gets generated on line 21, etc). I need to be able to easily remove what every text is generated inside those tags on those lines. Again, if I need to leave the tag set there empty, I'm fine with that.
Any thoughts?
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?
Search: Source Code
Find: (<(version|duration|titleNum)>)[^<]*(<\/\2>)
Replace with: $1$3
This does exactly what I was looking for. It removes the text from the tags on those lines that I don't want while preserving the text on other lines that I do. THANKS! This was very helpful!
I might ask one other question in hopes of tweaking this further...
Lets say I want to take your code above and use it to remove the necessary text and also, in the same swoop, replace text in a tag on another line?
For example, let's take this code again:
<version>5.0</version>
<studio>
</studio>
<language>
</language>
<movieReleaseYear>2007</movieReleaseYear>
<largeCoverParams>
</largeCoverParams>
<smallCoverParams>
</smallCoverParams>
<duration>121</duration>
<title>
<titleNum>1</titleNum>
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?
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>
North America
Europe, Middle East and Africa
Asia Pacific