Patrick Shannon
11-30-2007, 03:13 PM
I'm working on a client's new Sharepoint intranet all alone and they're wanting me to upload all of their news flash archives, which are 200 different pages. It's a royal bitch in Sharepoint to do this as it takes a while to add the pages ONE AT A TIME manually through the Sharepoint interface, major repetition.
What I did discover is that I could create my own .aspx pages through code and upload them that way, and the page would generate with the values filled in already. This is considerably quicker.
So that basically means I can create an .aspx template, then use a find and replace on different fields to replace the values for each story (like title, date, etc).
But it's still tedious to do a find and replace several times per story. This is what I would like. I would like to put all the different stories into a database, document or an XML file (most likely), then specify matching fields in the template document. Then I could do some sort of "import" with the XML/database file, and then the program or whatnot would automatically fill in these fields and build a static page (based on the template) for each data entry.
Does anyone know if there any way I can do this with Dreamweaver, a code editor or whatnot?
Yes, if I understand correctly.
However, you need to know php and DOM document navigation.
Do you know xml?
I did a similar thing with my portfolio.
for example:
<stories>
<story title="setting this up">
bdjakdnd asd d ajdn das d asddnasdda jd adajb.
</story>
<story title="getting it to work">
bdjakdnd asd d ajdn das d asddnasdda jd adajb.
</story>
<story title="finishing the set up">
bdjakdnd asd d ajdn das d asddnasdda jd adajb.
</story>
</stories>
Is that what you mean in xml terms?
Then when someone clicks on a link it loads the correct information for the clicked on story?
I could only tell you how to do it in php if your working with a compliant php5 hosting company though. The functions I use in my script are only available in php5.
The other thing since, you have so many stories its probably best to save the files as txt documents. Then inbetween the story tags place the name of the file. Then parse it with php. This way everytime the page loads the php parser wouldn't need to load all 200 story text.
This is an explaination followed by the exact text files to create to make it all work.
First, I created a xml document. My xml document just consists of a basic template for for stories and titles.
xml document
<?xml version="1.0" ?>
<stories>
<story title="the other moon">
This is a story about the moon.
</story>
<story title="the flip side">
This is a story about a coin.
</story>
<story title="all hail pancacakes">
This is a story about peoples love for panacakes.
</story>
<story title="tales from the freezer">
This is a story about a freezer.
</story>
<story title="blinds and shingles">
This is a story about the days and time of this guy and his.
</story>
<story title="cans in hands">
cansinhands.txt
</story>
</stories>
In the above xml document stories is the root tag. Inside stories each story is placed as a separate element tag. Each story element has a attribute called title. The title of the story is held in the title attribute. The content of a story is held inbetween the open and ends tags like so:
example
<story title="blinds and shingles">
This is a story about the days and time of this guy and his.
</story>
However, you can also place the name of a txt file there like so:
example text file
<story title="cans in hands">
cansinhands.txt
</story>
In the above example caninhand.txt is the name of the text file that contains the story text. I’ll have more info on that latter. However, the file must be a text file and have the txt extension to make this approach work.
Now, as for the index page it has been setup like so:
XHTML index page
<?php //php to read portfolio xml and set in variable $doc
$doc = new DOMDocument(); // create xml object
$doc->load('stories.xml'); // load xml doc into object
$root = $doc->documentElement; // get document root element <portfolio>
while($root->nodeType != 1) $root = $root->nextSibling; // makes sure we have root node not text element
$branches = $root->childNodes; //collects all child nodes in nodeList
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Stores</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
</head>
<body>
<?php createStoryMenu($branches); // php to show story menu ?>
<?php include("process-stories.php"); ?>
</body>
</html>
<?php // function to create Projectmenu
function createStorymenu($children) {
echo '<ul id="StoryMenu">'."\n"; // outputs opening tags of unordered list
foreach($children as $child) {
if($child->nodeType != 1) continue; // checks to make we are processing a element node
echo '<li><a href="'. $_SERVER['PHP_SELF'] . '?story='. urlencode(strtolower($child->attributes->getNamedItem('title')->value)).'"';
if(isset($_GET['story'])) { // checking to see what piece the user is currently viewing so I can highlight it
$story = str_replace('+',' ',$_GET['story']);
if(strcmp( strtolower($child->attributes->getNamedItem('title')->value), $story) == 0) echo ' class="highlight-story' ;
}
echo '">' . $child->attributes->getNamedItem('title')->value . '</a></li>'."\n"; // output mark-up for separate list items with link
}
echo '</ul> <!-- end of StoryMenu Unordered list -->'."\n"; //output closing tags for Projectmenu unordered List
}
?>
The bold parts in the above example outline the php code that makes this work. Its not important you know how this works but, what is important is this part of the above code:
XHTML index page segment
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Stores</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
</head>
<body>
<?php createStoryMenu($branches); // php to show story menu ?>
<?php include("process-stories.php"); ?>
</body>
</html>
The first highlighted piece creates an unordered list for the story menu. The second piece is an include that will output the story information. Therefore, you can place these pieces of code where you want the menu and story text to output in the structure of the document. They don’t need to be kept together.
The php include file is below:
php indluce file name- process-stories.php
<?php if(isset($_GET['story'])) climbTree(); else return; ?>
<?php /**/?>
<?php
function climbTree() { // places xml document in DOMDocument
$story = $_GET['story'];
$story = str_replace('+',' ',$story);
$doc = new DOMDocument(); // create DOM XML object
$doc->load("stories.xml"); // load document
getRootNode($doc,$story); // passes DOMDocument to new function
}?>
<?php
function getRootNode($doc,$story) { // gets the root element of document
$root = $doc->documentElement; // get document root node
while($root->nodeType != 1) $root = $root->nextSibling; // makes certain we have root node and not a text string
getStories($root,$story); // passes to new function to process
}
?>
<?php function getStories($root,$story) {
$storyNodes = $root->getElementsByTagName('story');
foreach($storyNodes as $pn) {
if($pn->nodeType != 1) continue;
if(strcmp(strtolower($pn->attributes->getnamedItem('title')->value), $story) != 0) continue;
$content = extractString($pn->nodeValue);
showStory($content,$pn); // show image in gallery
break; // only process one node
}
} ?>
<?php
function showStory($content,$main) {
echo '<h3>'. $main->attributes->getnamedItem('title')->value . '</h3>'."\n";
echo '<p>'. $content .'</p>'."\n";
}
?>
<?php
function extractString($node) { // extract information from text file and return it as string
if(substr_count($node,'.txt') >= 1) {
$filename = '/Users/zmijewski/Sites/stories/stories/'. $node;
if(@fopen($filename, "r")) {
$handle = @fopen($filename, "r");
$contents = @fread($handle, filesize($filename));
@fclose($handle);
return $contents;
} else { return "story not available"; }
} else return $node;
}?>
Its not important that you know how this works. However, it is important that you relaize this is the code that will output the story information and is included in the index page via the php include.
Furthermore, the bold highlight in this code represents the path to the textfiles. If you would like to use separate textfiles for each story then it is imperative that those textfiles reside in the path matching the bold highlight. therefore, you will need to delete the path I have there and place in the correct one to your textfiles.
With that the entire thing consist of three files.
- xml file
- index page
- php include
All these files should be located in the same folder else this won’t work.
Here are the files as named on my computer. The names of the xml and include file should not be changed else this won’t work.
You also need to send the index.php to the server to load in order for it to parse the xml.. You can’t just drag the index file to the browser. if you do this the entire thing won’t work.
Files:
1.)index.php - this is the index page
<?php //php to read portfolio xml and set in variable $doc
$doc = new DOMDocument(); // create xml object
$doc->load('stories.xml'); // load xml doc into object
$root = $doc->documentElement; // get document root element <portfolio>
while($root->nodeType != 1) $root = $root->nextSibling; // makes sure we have root node not text element
$branches = $root->childNodes; //collects all child nodes in nodeList
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Stores</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
</head>
<body>
<?php createStoryMenu($branches); // php to show story menu ?>
<?php include("process-stories.php"); ?>
</body>
</html>
<?php // function to create Projectmenu
function createStorymenu($children) {
echo '<ul id="StoryMenu">'."\n"; // outputs opening tags of unordered list
foreach($children as $child) {
if($child->nodeType != 1) continue; // checks to make we are processing a element node
echo '<li><a href="'. $_SERVER['PHP_SELF'] . '?story='. urlencode(strtolower($child->attributes->getNamedItem('title')->value)).'"';
if(isset($_GET['story'])) { // checking to see what piece the user is currently viewing so I can highlight it
$story = str_replace('+',' ',$_GET['story']);
if(strcmp( strtolower($child->attributes->getNamedItem('title')->value), $story) == 0) echo ' class="highlight-story' ;
}
echo '">' . $child->attributes->getNamedItem('title')->value . '</a></li>'."\n"; // output mark-up for separate list items with link
}
echo '</ul> <!-- end of StoryMenu Unordered list -->'."\n"; //output closing tags for Projectmenu unordered List
}
?>
2.)stories.xml - xml file. This is the file that you can add stories to and manipulate as needed.
<?xml version="1.0" ?>
<stories>
<story title="the moon">
This is a story about the moon.
</story>
<story title="the flip side">
This is a story about a coin.
</story>
<story title="all hail pancacakes">
This is a story about peoples love for panacakes.
</story>
<story title="tales from the freezer">
This is a story about a freezer.
</story>
<story title="blinds and shingles">
This is a story about the days and time of this guy and his.
</story>
<story title="cans in hands">
cansinhands.txt
</story>
</stories>
3.)process-stories - php include file
<?php if(isset($_GET['story'])) climbTree(); else return; ?>
<?php /**/?>
<?php
function climbTree() { // places xml document in DOMDocument
$story = $_GET['story'];
$story = str_replace('+',' ',$story);
$doc = new DOMDocument(); // create DOM XML object
$doc->load("stories.xml"); // load document
getRootNode($doc,$story); // passes DOMDocument to new function
}?>
<?php
function getRootNode($doc,$story) { // gets the root element of document
$root = $doc->documentElement; // get document root node
while($root->nodeType != 1) $root = $root->nextSibling; // makes certain we have root node and not a text string
getStories($root,$story); // passes to new function to process
}
?>
<?php function getStories($root,$story) {
$storyNodes = $root->getElementsByTagName('story');
foreach($storyNodes as $pn) {
if($pn->nodeType != 1) continue;
if(strcmp(strtolower($pn->attributes->getnamedItem('title')->value), $story) != 0) continue;
$content = extractString($pn->nodeValue);
showStory($content,$pn); // show image in gallery
break; // only process one node
}
} ?>
<?php
function showStory($content,$main) {
echo '<h3>'. $main->attributes->getnamedItem('title')->value . '</h3>'."\n";
echo '<p>'. $content .'</p>'."\n";
}
?>
<?php
function extractString($node) { // extract information from text file and return it as string
if(substr_count($node,'.txt') >= 1) {
$filename = '/Users/zmijewski/Sites/stories/stories/'. $node;
if(@fopen($filename, "r")) {
$handle = @fopen($filename, "r");
$contents = @fread($handle, filesize($filename));
@fclose($handle);
return $contents;
} else { return "story not available"; }
} else return $node;
}?>
This code can also be modified to add other features if you know how. However, if you don’t then I advise not messing with it to much. With that this is the basic structure for parsing xml with php. I'm sure there are other way but, that is the way I have structured my portfolio but, with many more element tags. This is a pretty simple version but, hope it helps.
hewligan
12-02-2007, 08:52 AM
What you're describing sounds almost exactly what XSLT (http://w3schools.com/xsl/default.asp) does. Mind you, XSLT can get a bit tricky.
If you have DW CS3 then you can achieve this with SPRY, which is Adobe's proprietry AJAX framework.