Jump to content

[SOLVED] Parsing Assistance Needed With XML Files


mykel241

Recommended Posts

Hello,

 

I need to find all occurances of <root contentId=" in this xml file and then be able to access the value of the content id via an array as needed.

 

Here is sample XML:

 

<webservices ver="1.0">

  <status code="1"/>

  <channel>

    <name>somename</name>

    <shortName>some shortname</shortName>

    <authors>

      <author id="17">Some author</author>

      </authors>

  </channel>

  <messageFlow>

        <title>Some Title</title>

    <link>http://www.somelink.com</link>

    <deliveryDate>2007-07-03 00:00:00.0</deliveryDate>

    <root contentId="291655">

      <numComments>0</numComments>

      <text>Some text</text>

      <tags/>

    </root>

  </messageFlow>

  <messageFlow>

    <title>Some Title</title>

    <link>http://www.somelink.com</link>

    <deliveryDate>2007-07-03 00:00:00.0</deliveryDate>

    <root contentId="291656">

      <numComments>0</numComments>

      <text>Some text</text>

      <tags/>

    </root>

  </messageFlow>

 

If anyone can help I would appreciate it. I would prefer to use regular expressions instead on a xml parser.

 

Michael :)

Link to comment
Share on other sites

<?php
$xml_file = file_get_contents('xmlfile.xml');

$root_split = split("</root>", $xml_file);

foreach ($root_split as $root) {
      if (eregi("\">", $root)) {
           list($ids[]) = split("\">", $root);
      }
}

print_r($ids);

?>

 

 

Also here is my shot at a regex version

 

<?php
$xml_file = file_get_contents('xmlfile.xml');

ereg('<root contentId=\"([0-9]*)\">', $xml_file, $matches);
print_r($matches);
?>

 

I am not very good with Regex, but hopefully the above is correct and works.

Link to comment
Share on other sites

<?php
$xml_file = file_get_contents('xmlfile.xml');

while (true) {
    if (ereg("<root contentId=\"([0-9]*)\">", $xml_file, $matches)) {
         $ids[] = $matches[1]; // I think thats right
         $xml_file = str_replace("<root contentId=\"" . $matches[1] . "\">", "", $xml_file);
    }else {
       break;
   }
}    

print_r($ids);
?>

 

Unsure if that will work, but yea. I thought there was a way that it would return all the results. But hopefully the above works and runs somewhat fast.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.