/    Sign up×
Community /Pin to ProfileBookmark

DOMDocument::loadXML() error

The error

[code]
Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Opening and ending tag mismatch: p line 1 and div in Entity, line: 8 in /var/www/gkt/includes/class_xmltoolbox.php on line 44

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Opening and ending tag mismatch: root line 1 and p in Entity, line: 10 in /var/www/gkt/includes/class_xmltoolbox.php on line 44

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Extra content at the end of the document in Entity, line: 10 in /var/www/gkt/includes/class_xmltoolbox.php on line 44

Fatal error: Call to a member function item() on a non-object in /var/www/gkt/includes/class_xmltoolbox.php on line 48
[/code]

The function

[code=php]
public function inparse($text)
{

// document for parsing text tree
$xml = DOMDocument::loadXML(‘<?xml version=”1.0″ encoding=”utf-8″?><root><p>’ . $text . ‘</p></root>’);

// container for loaded tree
$tree = $this->createDocumentFragment();
foreach( $xml->childNodes->item(0)->childNodes as $child)
{
$tree->appendChild( self::importNode($child) );
}
// returns parsed tree
return $tree;
}
[/code]

The string passed to the function

[code=html]
Personen: ******* har ansökt om att registera sig på gkt.<br />
Användarnamn: Anto<br />
Email konto: *******@hotmail.com<br />
Telephone: *******<br />
Född:20-09-1991<br />
<br />
Activerings link: <br />
Av activerings link: <a href=”http://www.katten.se”>link</a></div><br />

<br />
Vänliga hälsningar bearhardcreations.
[/code]

If i remove the link in the string sent to the function it works but i need it to work even if there is html in the string.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinOct 31.2007 — I'm pretty sure I've encountered this problem before. The annoying thing is that I can't remember exactly what caused it. All I can offer you is the code I use to do the same job. I'm not sure if my problem was more namespace-related or not, but here's my code anyway.

There's two functions I use; one function is for handling strings as input and the other is for handling files as input. The class they belong to is intended to be instantiated into an object, but you should be able to tweak them so that you can call them statically (at least that's how it looks like you're using the code you've posted so far).

I'm sorry to be so hopeless and vague, but I hope the code will help give you some ideas.

The string handling function is [code=php]//Take a string and parse it into a document element.
public function parse_string_into_element( DOMElement $element , $string , $namespace = NULL)
{
$fragment_document = new domDocument();

if(is_string($namespace) === TRUE)
{
$fragment_document->loadXML('<root xmlns="' . $namespace . '">' . $string . '</root>');

} else {

$fragment_document->loadXML('<root>' . $string . '</root>');
}

foreach($fragment_document->documentElement->childNodes as $child_node)
{
$child_node = $this->importNode($child_node , TRUE);
$element->appendChild($child_node);
}

return $document_fragment;
}[/code]
and the file handling function is [code=php]//Take a file and return it as a document fragment.
public function file_to_frag($file)
{
$fragment_document = new domDocument();
$fragment_document->load($_SERVER['DOCUMENT_ROOT'].$file);

$document_fragment = $this->createDocumentFragment();
$document_fragment->appendChild($this->importNode($fragment_document->documentElement , TRUE));

return $document_fragment;
}[/code]
Copy linkTweet thisAlerts:
@kattenauthorOct 31.2007 — I get almost the same error with your parse_string_into_element function
<i>
</i> &lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;: DOMDocument::loadXML() [&lt;a href='function.DOMDocument-loadXML'&gt;function.DOMDocument-loadXML&lt;/a&gt;]: Opening and ending tag mismatch: root line 1 and div in Entity, line: 8 in &lt;b&gt;/var/www/gkt/includes/class_xmltoolbox.php&lt;/b&gt; on line &lt;b&gt;52&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;: DOMDocument::loadXML() [&lt;a href='function.DOMDocument-loadXML'&gt;function.DOMDocument-loadXML&lt;/a&gt;]: Extra content at the end of the document in Entity, line: 8 in &lt;b&gt;/var/www/gkt/includes/class_xmltoolbox.php&lt;/b&gt; on line &lt;b&gt;52&lt;/b&gt;&lt;br /&gt;

&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;: Invalid argument supplied for foreach() in &lt;b&gt;/var/www/gkt/includes/class_xmltoolbox.php&lt;/b&gt; on line &lt;b&gt;55&lt;/b&gt;&lt;br /&gt;
Copy linkTweet thisAlerts:
@Stephen_PhilbinOct 31.2007 — Oh I've just noticed that last error you get. the one about the "class_xmltoolbox.php" file.

Is it ok for you to post that file so I can have a look at it please? I have a feeling it may be interfering with the DOMDocument class.

Thanks.
Copy linkTweet thisAlerts:
@kattenauthorOct 31.2007 — [code=php]<?php
class XMLToolBox
{
private $data;

public function __construct() {
$this->data = new DomDocument('1.0','utf-8');
$this->data->formatOutput = true;
}

public function createElement($element) {
return $this->data->createElement($element);
}

public function createTextNode($content)
{
return $this->data->createTextNode($content);
}

public function importNode($content) {
return $this->data->importNode($content);
}

public function saveXML(DOMNODE $node = null) {
return $this->data->saveXML($node);
}

public function loadHTML($source) {
return $this->data->loadHtml($source);
}

public function saveHTML() {
return $this->data->saveHTML();
}

public function createDocumentFragment()
{
return $this->data->createDocumentFragment();
}

public function inparse( DOMElement $element , $string , $namespace = NULL)
{
$fragment_document = new domDocument();

if(is_string($namespace) === TRUE)
{
$fragment_document->loadXML('<root xmlns="' . $namespace . '">' . $string . '</root>');

} else {

$fragment_document->loadXML('<root>' . $string . '</root>');
}

foreach($fragment_document->documentElement->childNodes as $child_node)
{
$child_node = $this->importNode($child_node , TRUE);
$element->appendChild($child_node);
}

return $document_fragment;
}
}
?>[/code]


Code that uses xmltoolbox

[code=php]
<?php
/**
* Display thread page
* @package viewthread
* @since 0.0.1
* @version 0.0.1
*/
define('THIS_SCRIPT','THREADS');
try
{
include('config.php');
include('./includes/const.php');
include('./includes/functions.php');
include('./includes/class_core.php');
include('./includes/functions_xml.php');
include('./includes/class_bbparser.php');

$db = $kraster->getInstance('database');
$template = $kraster->getInstance('template');
process_user_settings($kraster,$template);

$priviliges = parser_user_priviliges($kraster,'threads');
if($priviliges !== true)
{
throw new exception(constant($priviliges));
}

// ID definition of certain things
$groupID = $kraster->getInstance('session')->read('group');
$groupID = $groupID === false ? 0 : $groupID;
$uid = $kraster->getInstance('session')->read('uid');
$uid = $uid === false ? 0 : $uid;
$page = $kraster->getInstance('httpData')->read('page');
$page = empty($page) ? 0 : $page;
$id = $kraster->getInstance('httpData')->read('id');
$id = empty($id) ? 0 : $id;

$group = $kraster->getInstance('groups');
$group->setGroupData($db->query('SELECT * FROM groups WHERE id = '.$groupID)->fetch());
$thread = $db->query('SELECT * FROM threads WHERE id = '.(int) $id)->fetch();

if(empty($thread) || !is_numeric($id))
{
throw new exception(THREAD_DOSE_NOT_EXIST);
}

// This is abit of a waste of resources.
// What it dose is that it checks the category then board group access requirements for the current thread.
// Disabled by settings system.security level smaller then 2
if($kraster->getConfig('system.security') >= 2)
{
// Here we can use fetch because if a thread exists a board must exist for it.
$board = $db->query('SELECT groups,upperid FROM boards WHERE id = '.(int) $thread[0]['boardid'])->fetch();

// Access denied for the board
if(!(in_array($groupID, unserialize($board['groups'])) && strpos($board['groups'],'*') === false && $group->canView('all')))
{
throw new exception(PERMISSION_DENIED);
}

$category = $db->query('SELECT groups FROM categories WHERE position = '.$board['upperid'])->fetch();

if(!(in_array($groupID, unserialize($category['groups'])) && strpos($category['groups'],'*') === false && $group->canView('all')))
{
throw new exception(PERMISSION_DENIED);
}
}

$canViewDeleted = $group->canView('deleted') == 1 && in_array($groupID,$board[0]['groups']) ? '' : ' AND deleted = 'n'';
$posts = $db->query('SELECT * FROM posts WHERE threadid = '.$id.$canViewDeleted.' ORDER BY id DESC LIMIT 10 OFFSET 0')->fetchAll();



foreach($posts as $post)
{
$root = $template->xml->createElement('table');
setAttributes(array('width' => '440px','height' => 165,'border' => '1px solid #000','cellpadding' => 0,'cellspacing' => 0),$root);


/*
First row
*/
$tr = $template->xml->createElement('tr');

$td = $template->xml->createElement('td');
setAttributes(array('width' => 74,'align' => 'left','valign' => 'top','bgcolor' => '#FFFF00'),$td);
$td->nodeValue = '#'.$post['id'];
$tr->appendChild($td);

$td = $template->xml->createElement('td');
setAttributes(array('width' => 260,'height' => 21,'valign' => 'top','bgcolor' => '#FFFF00'),$td);
$td->nodeValue = $post['title'];
$tr->appendChild($td);

$td = $template->xml->createElement('td');
setAttributes(array('width' => 98,'align' => 'right','valign' => 'top','bgcolor' => '#FFFF00'),$td);
$td->nodeValue = date('d-m-y H:i',$post['date']);
$tr->appendChild($td);
$root->appendChild($tr);


/*
Second row
*/

$user = $db->query('SELECT id,name,avatar FROM users WHERE id = '.(int) $post['uid'])->fetch();
$avatar = $user['avatar'] != NULL ? $user['name'].'.'.$user['avatar'] : 'bildsaknas.jpg';
$tr = $template->xml->createElement('tr');

$td = $template->xml->createElement('td');
setAttributes(array('width' => 74,'align' => 'left','valign' => 'top'),$td);
$td->appendChild($template->html->img('./img/users/'.$avatar,$user['avatar'],false,'width:70px;'));
$td->appendChild($template->xml->createElement('br'));
$td->appendChild($template->html->createLink($user['name'],array('href' => 'profile.php?id='.$user['id'])));
$tr->appendChild($td);

$td = $template->xml->createElement('td');
setAttributes(array('width' => 121,'colspan' => 2,'align' => 'left','valign' => 'top'),$td);
$template->xml->inparse($td,BBParser::parse($post['message']));
$tr->appendChild($td);
$root->appendChild($tr);
$template->appendTree($root);
$template->appendTree($template->xml->createElement('br'));
}
}
catch(exception $e)
{
var_dump($e);
}
$template->display();
?>

[/code]
Copy linkTweet thisAlerts:
@wrzasqOct 31.2007 — nah... guys it's just problem with passing HTML code ?.

&lt;a href="http://www.katten.se"&gt;link&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
here it is. the [i]</div>[/i] appears from nowhere.
Copy linkTweet thisAlerts:
@kattenauthorOct 31.2007 — Wow how stupid but also so strange, wrzasq i'm using the bbparser from OTSCMS its not realy working propaly.
Copy linkTweet thisAlerts:
@kattenauthorOct 31.2007 — Wow this is realy weird not a single error but no html output.
×

Success!

Help @katten spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...