-1

I need help regarding saving XML data into MySQL database. Here is my code:

    <?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>

Now what I wanted to do is to get the value of the <Description> tag and the <URL> tag and combine them to become a complete url then save it to mysql database.

halfer
  • 19,824
  • 17
  • 99
  • 186
vince
  • 13
  • 5

1 Answers1

1

See question here How do you parse and process HTML/XML in PHP?

You can use SimpleXML (see http://php.net/manual/en/simplexml.examples-basic.php) for parsing

$xmlStr = '<?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>';

$response = new SimpleXMLElement($xmlStr);

$url = (string) $response->Description . (string) $response->URL;

$url will contain:

http://sample.net/Patient/PatientView.aspx?pid=642

Then use PDO (http://php.net/manual/en/book.pdo.php) to store data into database:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $stmt = $dbh->prepare("INSERT INTO sample (url) VALUES (:url)");
    $stmt->bindParam(':url', $url);
    $stmt->execute();
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
Sergei Kasatkin
  • 385
  • 2
  • 11