0

I have some sort of ... xml document as below :

<file src="136090000-136100000">
  <member id="136090000">
        <Name>
              <![CDATA[DOSEKUN DANIEL ADETUNJI]]>
              </Name>
        <Email>
              <![CDATA[[email protected]]]>
              </Email>
        <DateRegistration>
              <![CDATA[10/19/2010 8:46:57 PM]]>
              </DateRegistration>
  </Member>
  <usafisMember id="136090001">
        <Name>
              <![CDATA[yoleida colina]]>
              </Name>
        <Email>
              <![CDATA[[email protected]]]>
              </Email>
        <DateRegistration>
              <![CDATA[10/19/2010 8:47:08 PM]]>
              </DateRegistration>
  </usafisMember>
  <usafisMember id="136090002">
        <Name>
              <![CDATA[Homero Valdovinos]]>
              </Name>
        <Email>
              <![CDATA[[email protected]]]>
              </Email>
        <DateRegistration>
              <![CDATA[10/19/2010 8:47:01 PM]]>
              </DateRegistration>
  </Member>

I need to extract the member id , name, email and DateRegistration but as you can see the xml is not very well formatted . I tried with simple xml which obviously doesn't work but nether regex doesn't seem to work . Here is the pattern that I tried :

$pattern = "/<Name><\\!\\[CDATA\\[(.*)\\]\\]><\/Name>/";
preg_match_all($pattern, $xml_content, $matches);
$name = $matches[1][0][v];
echo " name is $name ";
4
  • 3
    Use an XML parser, like SimpleXML. Commented Jan 3, 2011 at 4:09
  • Sorry for being obvious, but why don't you use ru2.php.net/simplexml? Commented Jan 3, 2011 at 4:09
  • 3
    Is the XML always missing the closing file tag or could there be any other invalid constructs? Commented Jan 3, 2011 at 4:09
  • I have tried "$xml_content = file_get_contents("/var/www/html/xml/$file_name"); //print_r($xml_content); $xml = simplexml_load_file("$xml_content"); echo $xml->getName() . "<br />"; " and here is the log file pastebin.com/674yGpsE Commented Jan 3, 2011 at 4:14

2 Answers 2

3

Parsing XML with regex is almost certainly wrong. Use a dedicated XML parser. There are plenty available for php.

Sign up to request clarification or add additional context in comments.

2 Comments

XML cannot be either well-formatted or not well-formatted; it can only be valid. If it's not valid XML then you ought to find XML that is, otherwise you are never going to be able to do this reliably.
(Your XML looks ok to me at first glance, apart from the missing closing tag.)
0
    $x='<file src="136090000-136100000">
      <usafisMember id="136090000">
            <Name>
                  <![CDATA[DOSEKUN DANIEL ADETUNJI]]>
                  </Name>
            <Email>
                  <![CDATA[[email protected]]]>
                  </Email>
            <DateRegistration>
                  <![CDATA[10/19/2010 8:46:57 PM]]>
                  </DateRegistration>
      </usafisMember>
      <usafisMember id="136090001">
            <Name>
                  <![CDATA[yoleida colina]]>
                  </Name>
            <Email>
                  <![CDATA[[email protected]]]>
                  </Email>
            <DateRegistration>
                  <![CDATA[10/19/2010 8:47:08 PM]]>
                  </DateRegistration>
      </usafisMember>
      <usafisMember id="136090002">
            <Name>
                  <![CDATA[Homero Valdovinos]]>
                  </Name>
            <Email>
                  <![CDATA[[email protected]]]>
                  </Email>
            <DateRegistration>
                  <![CDATA[10/19/2010 8:47:01 PM]]>
                  </DateRegistration>
      </usafisMember>
        </file>
    ';

    $xml = new SimpleXMLElement($x);



foreach ($xml->usafisMember as $usafisMember){
echo 'Name: '. $usafisMember->Name.'<br>';
}

had to add close file but other wise worked as expected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.