Need some help with XML composition.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danfm
Forum Newbie
Posts: 5
Joined: Thu Jun 28, 2007 7:26 pm

Need some help with XML composition.

Post by danfm »

I just dont get why this does't work...

XMLStory is a subset of XMLBlog. XMLStory has two children, story_teaser, and story_body.
I have a handful of other tags in the XML document...but for some reason $XMLStoryBody->appendChild( $dom->createTextNode($story) ); does not work.

If I put something directly in quotes into the createTextNode function, it will show up in the xml file I save. But not if I use the $story variable. I see the tags <story_body></story_body> in the XML file, but no text. Please help

Code: Select all

//STORY//
	$XMLStory = $XMLBlog->appendChild($dom->createElement('story'));

	$XMLTeaser = $XMLStory->appendChild($dom->createElement('story_teaser'));
	$XMLTeaser->appendChild( $dom->createTextNode($teaser) );

	echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";

	$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));
	$XMLStoryBody->appendChild( $dom->createTextNode($story) );




//Get the content
$story = $_POST['story'];
$title = $_POST['title'];
$subject = $_POST['subject'];
$teaser = $_POST['teaser'];
$author = $_POST['author'];

$story = str_replace( "\n","<P>",$story);

echo "HELLO WORLD<P>";

 echo "<P>Thank you for submitting your article.<P>";
 
echo "<P>You submitted:<BR>";
echo "<B>$title</B><BR><I>$author</I><BR>$story</P>";
 
 
 
//DO TITLE
if (strlen($title) <1 ) 
	die ("Need title");
	
$title = stripslashes($title);
$title = ereg_replace("[^[]+^\ ]", "", $title);	//Replace non alpha   |^[' ']

$filename = str_replace( ' ', '_', $title);

$title = CleanTags($title);

$filename =CleanTags($filename);

echo "<P>Your file is saved as $filename</P>";

//TEASER	
if( strlen($teaser)<1) 
{
	//No teaser, get first 100 words from story.
	$storyTemp = $story;
	$storyTemp = CleanTags($storyTemp);
	$storyTemp = stripslashes($storyTemp);
	
	$start = 0; $end = 0;
	$fstr = "."; $ctr = 0; $loc = 0;
		
	
	//Get the first x sentances of the story based on teaser sentence count
	while ( $ctr < TEASER_SEN_COUNT /*and $loc < strlen($Story) */)
	{	
		$loc = strpos( $storyTemp, $fstr, $loc+1 );
		$ctr++;	
	}
	
	echo "\n";echo "LOC: "; echo $loc; echo "\n";
	
	//Get the teaser up to where the last counter loc was.  T_W_C words or less.
	$teaser = substr($storyTemp, 1, $loc);
}//TEASER DONE

/***********************
** DOM FOR BLOGS
** <Blog>
** <Title />   
** <Subject />
** <Author />
** <Teaser />
** <Gen-Info>
** 	<Hits />
** <Story />
**<Comment sheet />
***************************/
	
	//Creates XML string and XML document using the DOM 
	$dom = new DomDocument('1.0'); 

	//Blog and sub cat info
	$XMLBlog = $dom->appendChild($dom->createElement('blog'));
	$XMLInfo = $XMLBlog->appendChild($dom->createElement('info'));
	
	//Information elements
	//Add title element to blog
	$XMLTitle = $XMLInfo->appendChild($dom->createElement('info_title'));
	$XMLTitle->appendChild( $dom->createTextNode($title) );
	
	$XMLAuthor = $XMLInfo->appendChild($dom->createElement('info_author'));
	$XMLAuthor->appendChild( $dom->createTextNode($author) );
	
	$XMLFilename = $XMLInfo->appendChild($dom->createElement('info_filename'));
	$XMLFilename->appendChild( $dom->createTextNode($filename) );
	////
	
	//STORY//
	$XMLStory = $XMLBlog->appendChild($dom->createElement('story'));

	$XMLTeaser = $XMLStory->appendChild($dom->createElement('story_teaser'));
	$XMLTeaser->appendChild( $dom->createTextNode($teaser) );

	echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";

	$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));
	$XMLStoryBody->appendChild( $dom->createTextNode($story) );
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

meaning $XMLStoryBody->appendChild( $dom->createTextNode('xyz') ); works
but $XMLStoryBody->appendChild( $dom->createTextNode($story) ); doesn't?

please try

Code: Select all

echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";
$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));
$dbgText = ' - ' . gettype($story) . ' ' . strlen($story) . ' ' . $story . ' - ';
$XMLStoryBody->appendChild( $dom->createTextNode($dbgText) );
danfm
Forum Newbie
Posts: 5
Joined: Thu Jun 28, 2007 7:26 pm

Post by danfm »

I changed it and echoed the debug variable.

Code: Select all

echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";
$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));

$dbgText = ' - ' . gettype($story) . ' ' . strlen($story) . ' ' . $story . ' - ';
echo "<P>dbg - $dbgText<P>";

$XMLStoryBody->appendChild( $dom->createTextNode($dbgText) );

dbg - - string 4066

but then I checked the xml output file:
<story_teaser>ave you ever wondered how some websites offer you different choices so you can select a theme? Wonder no more. Follow this simple tutorial and you can offer your users control over the look of your website. You can break it down into</story_teaser>

<story_body></story_body>
</story>
</blog>


[/quote]


And yes, it does enter the text into the actual document if it is put into createTextNode("some text") but not with a $variable Also the whitespacing around the -> is correct.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

danfm wrote:Also the whitespacing around the -> is correct.
No idea what you mean but could you please try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);

echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";
$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));

$dbgText = ' mary had a little lamb ';
$XMLStoryBody->appendChild( $dom->createTextNode(dbgText) ); 
$XMLStoryBody->appendChild( $dom->createTextNode($story) ); 
$XMLStoryBody->appendChild( $dom->createTextNode(' --- ') );
danfm
Forum Newbie
Posts: 5
Joined: Thu Jun 28, 2007 7:26 pm

These are the error messages

Post by danfm »

These are the error messages. I think I am seeing the problem. Is it in an incompatible format? Thanks for the continued help.


Warning: DOMDocument::saveXML() [function.DOMDocument-saveXML]: string is not in UTF-8 in /services/webpages/c/o/collinsreport.net/public/SaveStory.php on line 130

FILENAME: ./blogs/Sounded_too_much_like_Springtime_for_Hitler.xml


Warning: DOMDocument::save() [function.DOMDocument-save]: string is not in UTF-8 in /services/webpages/c/o/collinsreport.net/public/SaveStory.php on line 136

Code: Select all

echo "<P>#####################<P>STORY:<P>$story<P>$$$$$$$$<P>";
$XMLStoryBody = $XMLStory->appendChild($dom->createElement('story_body'));

$dbgText = ' mary had a little lamb ';
$XMLStoryBody->appendChild( $dom->createTextNode($dbgText) );
$XMLStoryBody->appendChild( $dom->createTextNode($story) );
$XMLStoryBody->appendChild( $dom->createTextNode(' --- ') );	

echo "<P>END DEBUG BLOCK<P>";




<story_body> mary had a little lamb --- </story_body>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might be interested in
http://de2.php.net/utf8_encode wrote:utf8_encode — Encodes an ISO-8859-1 string to UTF-8
or
http://de2.php.net/mb_convert_encoding wrote:mb_convert_encoding — Convert character encoding
danfm
Forum Newbie
Posts: 5
Joined: Thu Jun 28, 2007 7:26 pm

Thanks

Post by danfm »

Thanks a million.

-Dan
danfm
Forum Newbie
Posts: 5
Joined: Thu Jun 28, 2007 7:26 pm

Post by danfm »

utf8_encode ($story);

Totally works. Thanks again
Post Reply