title: How to fix Fatal error: Uncaught Error: Call to a member function children() on string
body: <p>I get the error Fatal error: Uncaught Error: Call to a member function children() on string when i try to parse an xml webpage and write the data to a mysql database table</p>
  
  <p>I tried to do this by using the below code
  Can anyone please advice me on the right way to parse an xml webpage endpoint and write the data to a mysql database table</p>
  
  <pre><code>&lt;?php
  function sanitize_for_xml($input) {
      // Convert input to UTF-8.
      $old_setting = ini_set('mbstring.substitute_character', '"none"');
      $input = mb_convert_encoding($input, 'UTF-8', 'auto');
      ini_set('mbstring.substitute_character', $old_setting);
  
      // Use fast preg_replace. If failure, use slower chr =&gt; int =&gt; chr conversion.
      $output = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $input);
      if (is_null($output)) {
          // Convert to ints.
          // Convert ints back into a string.
          $output = ords_to_utfstring(utfstring_to_ords($input), TRUE);
      }
      return $output;
  
  }
  ?&gt;
  &lt;?php
  
                          $affectedRow = 0;
                          $url ="https://ngcareers.com/xmlfeed.xml";
                          $xml = simplexml_load_file($url) or die("Error: Cannot create object");
                          $xml = sanitize_for_xml($xml);
                          echo "xml is ".$xml;
  
                          $xml = str_replace("[^\\x20-\\x7e]", "",$xml);
                          $xml = str_replace("&lt;b&gt;", "",$xml);
                          $xml = str_replace("&lt;u&gt;", "",$xml);
                          $xml = str_replace("&lt;/u&gt;", "",$xml);
                          $xml = str_replace("&lt;providerUrl&gt;", "",$xml);
                          $xml = str_replace("&lt;/providerUrl&gt;", "",$xml);
                          $xml = str_replace("&amp;lsquo;", "",$xml);
                          $xml = str_replace("&amp;amp;", "",$xml);
                          $xml = str_replace("&amp;rsquo;", "",$xml);
                          $xml = str_replace("&amp;bull;", "",$xml);
                          $xml = str_replace("&lt;strong&gt;", "",$xml);
                          $xml = str_replace("&lt;/strong&gt;", "",$xml);
                          $xml = str_replace("&lt;p&gt;", "",$xml);
                          $xml = str_replace("&lt;/p&gt;", "",$xml);
                          $xml = str_replace("&lt;br /&gt;", "",$xml);
                          $xml = str_replace("&lt;/b&gt;", "",$xml);
                          $xml = str_replace("&lt;li&gt;", "",$xml);
                          $xml = str_replace("&lt;/li&gt;", "",$xml);
                          $xml = str_replace("&lt;ul&gt;", "",$xml);
  
                          $xml = str_replace("&lt;/ul&gt;", "",$xml);
                          $xml = str_replace("&amp;#39;", "",$xml);
                          $xml = str_replace("&amp;nbsp;", "",$xml);
                          $xml = str_replace("&amp;quot;", "",$xml);
                          $xml = str_replace("&lt;!--[endif]--&gt;", "",$xml);
                          $xml = str_replace("&lt;!--[if !supportLists]--&gt;;", "",$xml);
  
                          foreach ($xml-&gt;children() as $row) {
                              $title = $row-&gt;title;
                              $link = $row-&gt;url;
                              $description = $row-&gt;description;
                              $date = $row-&gt;date;
                              $city = $row-&gt;city;
                              $state = $row-&gt;state;
                              $country = $row-&gt;country;
                              $company = $row-&gt;company;
                              $requirements= $row-&gt;requirements;
                              $category = $row-&gt;category;
                              $experience = $row-&gt;experience;
                              $salary = $row-&gt;salary;
                              $id = $row-&gt;id;
  
                              $sql = "INSERT INTO requirement(title,link,description,
                              jobdate,city,state,
                              country,company,requirements,
                              category,experience,salary,jobid) VALUES ('" . $title . "','" . $link . "','" . $description .
                                  "','" . $date .  "','" . $city . "','" . $state . "','" . $country . "','" . $company . "','" .
                                  $requirements . "','" . $category . "','" . $experience . "','" . $salary . "','" . $id
                                  . "')";
  
                              $result = mysqli_query($con, $sql);
  
                              if (! empty($result)) {
                                  $affectedRow ++;
                              } else {
                                  $error_message = mysqli_error($con) . "\n";
                              }
                          }
                          ?&gt;
                          &lt;h2&gt;Insert Job Data to  Table &lt;/h2&gt;
                          &lt;?php
                          if ($affectedRow &gt; 0) {
                              $message = $affectedRow . " records inserted";
                          } else {
                              $message = "No records inserted";
                          }
  
                          ?&gt;
  </code></pre>