2013-04-23 140 views
-2

我一直在從事涉及從XML文件讀取的PHP頁面。但是,我看到了此錯誤消息:解析錯誤:解析錯誤,第24行中出現意外的T_OBJECT_OPERATOR。但我不確定導致錯誤的原因。所以我想知道是否有人可以弄清楚我做錯了什麼。php和xml解析錯誤:解析錯誤,意外T_OBJECT_OPERATOR

<?php 
if (!isset($_GET["searchstr"])) 
    print "no Search String is provided! <br />"; 
else if (!isset($_GET["searchtype"]) || empty($_GET["searchtype"])) 
    print "No Search type is selected!<br />"; 


    else 
{ 
    $searchstr = $_GET["searchstr"]; 
    $searchtype = $_GET["searchtype"]; 

    $xmlDoc = new DOMDocument(); 
    @ $success = $xmlDoc->load("books.xml"); 
    if (!$success) 
     print "No XML book data on the server!"; 


else 
{ 
    $books = $xmlDoc->getElementsByTagName("book"); 
    $num = 0; 
    foreach ($books as $book) 

     $title = $book->getElementsByTagName("title")->item(0)->firstChild->nodevalue;   
     $isbn13 = $book->getElementsByTagName("ISBN13")->item(0)->firstchild->nodevalue; 
     $isbn10t = $book->getElementsByTagName("ISBN10"); 
     $isbn10 = ""; 
    if ($isbn10t->length > 0) 
     $isbn10 = $isbn10t->item(0)->firstchild->nodevalue; 

     $authors = $book->getElementsByTagName("author"); 
     $author = ""; 
     for ($j=0; $j<$authors->length; $j++) 
     { 

if ($j>0) 
      { 
       if ($j < $authors->length-1) $author .= ". "; 
       else $author .= " and "; 
      } 
      $author .= $author->item($j)->getElementsByTagName("firstname")->item(0)->firstchild->nodevalue; 
      $author .= " " . $authors->item($j)->getElementsByTagName("lastname")-item(0)->firstchild->nodevalue; 
     } 
     $desc = $book->getElementsByTagName("descritpion"); 
     $description = ""; 
     if ($desc->length > 0) 
      $descreption = $desc->item(0)->firstchild->nodevalue; 

     if (empty($searchstr) 
     || searchtype == "title" && eregi($searchstr,$title) 
     || $searchtype == "isbn" && (eregi($searchstr, $author) 
     || $searchtype == "keyword" && (eregi($searchstr, $title) || eregi($searchstr, $description) 

{ 
      $num++ 
      print "{$num}. Title: <a href=\"select.php?isbn={$isb13}\">{$title}</a><br />"; 
      print "Author: {$author}<br />" 
      print "ISBN-13. "; 
      print $books->getElementsByTagName("ISBN13")->item(0)->firstchild->nodevalue."<br />"; 
      $isbn10 = $book->getElementsByTagName("ISBN10"); 
      if ($isbn10->length > 0) 
      { 
       print " ISBN-10: " . $isbn10->item(0)->firstchild-nodevalue. "<br />"; 
      } 
      print "Price: $"; 
      print $book->getElementsByTagName("price")->item(0)->firstchild->nodevalue. "<br />"; 
      print "<br />" 
     } 
     if ($num == 0) 
      print "no book is found!"; 
    } 


} 
} 

?> 

這裏是我使用

<book id="1"> 
    <title>Beginning ASP.NET 4 in C# 2010</title> 
    <authors> 
     <author> 
     <lastname>MacDonald</lastname> 
     <firstname>Matthew</firstname> 
     </author> 
    </authors> 
    <ISBNs> 
     <ISBN13>9781430226086</ISBN13> 
     <ISBN10>1430226080</ISBN10> 
    </ISBNs> 
    <publisher>Apress</publisher> 
    <publishdate>2010-05</publishdate> 
    <pages>981</pages> 
    <price>49.99</price> 
    <description>This book discusses ASP.NET programming in C# with Visual Studio 2010.</description> 
    </book> 

這裏是從搜索頁面的代碼,PHP頁面之前的頁面的XML代碼的一個例子。

<!DOCTYPE html> 
<html> 
<head> 
    <title>Textbook Buyer</title> 
    <link href="styles.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script type="text/javascript"> 

    function doSearch() 
    { 
     var searchstr = document.getElementById("searchstr").value; 
     var searchtype = document.getElementById("searchtype").value; 

     $.ajax 
     ({ 
      type: "GET", 
      url: "searchlist.php?searchstr=" + searchstr + "&searchtype=" + searchtype, 
      success: function(sText, sStatus, oXHR) 
      { 
       $("div#results").html(sText); 
      }, 
      error: function (oXHR, sStatus, sError) 
      { 
       $("div#results").html("An error occurred."); 
      } 
     }); 
    } 

    </script> 
</head> 
<body> 
    <table> 
    <tr> 
     <td class="header" colspan="5">Textbook Buyer</td> 
    </tr> 
    <tr> 
     <td id="td1" colspan="5"> 
     | <a href="welcome.php">Welcome</a> 
     | <a href="bestsellers.php">Bestsellers</a> 
     | <a href="hotdeals.php">Hot Deals</a> 
     | <a href="bookstores.php">Bookstores</a> 
     | <a href="myaccout.php">My Account</a> 
     | <a href="help.php">Help</a> 
     |</td> 
    </tr> 
    <tr> 
     <td class="column"> 
     <strong>Browse Textbooks<br /> 
     by Category:</strong><br /> 
     <br /> 
     Business -<br /> 
     Computer Science -<br /> 
     History -<br /> 
     Mathematics -<br /> 
     Medical -<br /> 
     Social Science -<br /> 
     </td> 
     <td class="td2"></td> 
     <td id="td3"> 
     <div id="searchBox"> <br /> 
      <input type="text" id="searchstr" size="30" /> 
      <select id="searchtype"> 
      <option value="title" selected="selected">Title</option> 
      <option value="author">Author</option> 
      <option value="isbn">ISBN</option> 
      <option value="keyword">Keyword</option> 
      </select> 
      <br /> 
      <input type="button" id="searchButton" value="Search" onClick="doSearch();" /><br /> 

     </div> 
     <div id="results"></div> 
     </td> 
     <td class="td2"></td> 
     <td class="column"> 
     <strong>Customer Support:</strong><br /> 
     <br /> 
     Order Status<br /> 
     Customer Service <br /><br /> 
     <a href="search.php">Search Books</a> 
     </td> 
    </tr> 
    <tr> 
     <td id="td5" colspan="5">&copy; Copyright 2013, Textbook Buyer, Inc. All rights reserved. 
     </td> 
    </tr> 
    </table> 

</body> 
</html> 
+2

該錯誤消息通常包含一個行號。看那裏的問題。我們無法分辨,因爲您遺漏了這個珍聞。反正本地化也是如此。 – mario 2013-04-23 21:44:14

+0

您可能還想發佈一個給您提供問題的XML示例。 – pilsetnieks 2013-04-23 21:50:52

+0

我得到一個_「PHP解析錯誤:語法錯誤,意外的'{'」_你的代碼43行(未關閉,如果語句)。看看方便的線路號碼是多少? – Wrikken 2013-04-23 21:58:43

回答

1

您在這幾個代碼語法錯誤:

foreach ($books as $book) // this is invalid given the lines below it and the lines at 
// the end of the page. You forgot to open this: 

foreach ($books as $book) { //this is what it should be. 

$author .= $author->item($j)->getElementsByTagName("firstname")->item(0)->firstchild->nodevalue; 
$author .= " " . $authors->item($j)->getElementsByTagName("lastname")-item(0)->firstchild->nodevalue; 

$isbn10 = $isbn10t->item(0)->firstchild->nodevalue; 

$descreption = $desc->item(0)->firstchild->nodevalue; 

print $books->getElementsByTagName("ISBN13")->item(0)->firstchild->nodevalue."<br />"; 
//  ^not to mention, you need to delete this S ^ and ^both need to be upper-case 

// The above lines(and pretty much every single instance in which you use those two 
// methods) are using an incorrect property. nodevalue should be nodeValue, 
// firstchild should be firstChild. The capital C/V makes all the difference. 

$num++ 
    //^no semi-colon 

print "Author: {$author}<br />" 
          //^no semi-colon 

print "<br />" 
     // ^again, no semi-colon 

// the below.. 

$author .= $author->item($j)->getElementsByTagName("firstname")->item(0)->firstchild->nodevalue; 

//should be this: 

$author .= $authors->item($j)->getElementsByTagName("firstname")->item(0)->firstchild->nodevalue; 
//    ^notice the s here 

$author .= " " . $authors->item($j)->getElementsByTagName("lastname")-item(0)->firstchild->nodevalue; 
//                 ^forgot a key character, the > 

此:

if (empty($searchstr) 
    || searchtype == "title" && eregi($searchstr,$title) 
    || $searchtype == "isbn" && (eregi($searchstr, $author) 
    || $searchtype == "keyword" && (eregi($searchstr, $title) || eregi($searchstr, $description) 

應該是類似於..(記住關閉() S)(這是主要是猜測,因爲我不知道這個代碼的目的):

if (empty($searchstr) 
    || searchtype == "title" && eregi($searchstr,$title) 
// ^forgot the $ here, unless you have a constant called 'searchtype' 
    || $searchtype == "isbn" && (eregi($searchstr, $author)) 
    || $searchtype == "keyword" && (eregi($searchstr, $title) || eregi($searchstr, $description))) 

最後,就在這裏:

print " ISBN-10: " . $isbn10->item(0)->firstchild-nodevalue . "<br />"; 
// The C and V need to be upper case and  ^you forgot the > 

與所有固定這些錯誤,代碼爲我工作得很好。

+0

你也忘了定義一個你稍後使用的變量,在另一個塊中。你會得到一個通知告訴你這一點,但它不會阻止你的代碼工作,所以我沒有包含它。 – Daedalus 2013-04-23 23:43:07

+0

我不知道爲什麼,但似乎儘管你做了這些改變,但它仍然告訴我,我有一個解析錯誤:解析錯誤,第24行出現意外的T_OBJECT_OPERATOR。 – herkles 2013-04-24 00:27:26

+0

@herkles刪除第24行和周圍的行,重新打字。這可能是一個隱藏的角色從您複製的地方複製而來。 – Daedalus 2013-04-24 00:42:18