Help please how to create patern for this

Discussion in 'PHP' started by PinoyIto, Apr 17, 2007.

  1. #1
    Hello,

    Will you please help me to figure out how to create a patern for the following datas. So that using preg_match_all I can extract the the datas like catName , Id, Title, and Descriptiption. Thanks in advance

    
    <Category>
    <catName>Business to Business</catName>
    <item>
    <Id>25</Id>
    <Title> title will be here</Title>
    <Description>Descript will be here</Description>
    </item>
    
    <item>
    <Id>2</Id>
    <Title> title will be here</Title>
    <Description>Descript will be here</Description>
    </item>
    
    
    <item>
    <Id>5</Id>
    <Title> title will be here</Title>
    <Description>Descript will be here</Description>
    </item>
    </Category>
    
    <Category>
    <catName>Onother Category</catName>
    <item>
    <Id>5</Id>
    <Title>title will be here</Title>
    <Description>Descript will be here</Description>
    </item>
    
    <item>
    <Id>5</Id>
    <Title>title will be here</Title>
    <Description>Descript will be here</Description>
    </item>
    </Category>
    
    Code (markup):
    And so on
     
    PinoyIto, Apr 17, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Apr 17, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Do you have php5 ?
     
    krakjoe, Apr 17, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    ^^ Sorry for being off-topic, but why were you banned?
     
    nico_swd, Apr 17, 2007 IP
  5. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #5
    Yes I am using php 5 and I tried the following code
    
    <?php
    $xmlstr = simplexml_load_file('rssfeed.xml');
    $xml = new SimpleXMLElement($xmlstr);
    foreach ($xml->Category as $category) {
       echo $category->catName, '<br />';
    }
    ?>
    
    Code (markup):
    Just to test if it works but what I get is the following errors
    
    Warning: SimpleXMLElement::__construct() [function.--construct]: Entity: line 11: parser error : Start tag expected, '<' not found in /home/articnet/public_html/tools/latestnews.php on line 3
    
    Warning: SimpleXMLElement::__construct() [function.--construct]: in /home/articnet/public_html/tools/latestnews.php on line 3
    
    Warning: SimpleXMLElement::__construct() [function.--construct]: ^ in /home/articnet/public_html/tools/latestnews.php on line 3
    
    Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/articnet/public_html/tools/latestnews.php:3 Stack trace: #0 /home/articnet/public_html/tools/latestnews.php(3): SimpleXMLElement->__construct('??????????') #1 {main} thrown in /home/articnet/public_html/tools/latestnews.php on line 3
    
    Code (markup):
     
    PinoyIto, Apr 17, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    krakjoe, Apr 17, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    EDIT:

    Lol, makes sense. :p

    Aaand I think I just did.


    Even simpler:
    
    $xml = simplexml_load_file('rssfeed.xml');
    
    foreach ($xml->item as $category)
    {
       echo $category->Id, '<br />';
       echo $category->Title, '<br />';
       echo $category->Description, '<br />';
    }
    
    
    PHP:
     
    nico_swd, Apr 17, 2007 IP