How to: Easily parse XML with PHP

There are so many packages and scripts online to help a person parse XML. Unless your parsing some insanely complex files, there is no need. Its as easy as this:

Code:
$batch = new DOMDocument();
$batch->load($this->webPath);
$emails = $batch->getElementsByTagName("email");
foreach( $emails as $email ) {
  $to = $email->getElementsByTagName("to")->item(0)->nodeValue;
  $cc = $email->getElementsByTagName("cc")->item(0)->nodeValue;
  $bcc = $email->getElementsByTagName("bcc")->item(0)->nodeValue;
  $from = $email->getElementsByTagName("from")->item(0)->nodeValue;
  $subject = $email->getElementsByTagName("subject")->item(0)->nodeValue;
  $body = $email->getElementsByTagName("body")->item(0)->nodeValue;
}

Related Articles:

Follow me on Twitter!

Your Ad Here

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>