Results 1 to 3 of 3

Thread: xml, attributes

  1. #1
    Join Date
    Jan 2004
    Posts
    60

    xml, attributes

    Hello, I use this xml parser.. but I can't select "attributes".. any thoughts?


    you will notice "while ($node = GetElementByName($data, "<article>", "</article>"))" well obviously this doesn't work since there is an attribute in the <article> thing :-), I have no idea how I can get this attribute. The parser works great without attributes though.

    Thanks a lot!


    [/PHP]
    $file = "xmlnews.html";


    function GetElementByName ($xml, $start, $end) {

    global $pos;
    $startpos = strpos($xml, $start);
    if ($startpos === false) {
    return false;
    }
    $endpos = strpos($xml, $end);
    $endpos = $endpos+strlen($end);
    $pos = $endpos;
    $endpos = $endpos-$startpos;
    $endpos = $endpos - strlen($end);
    $tag = substr ($xml, $startpos, $endpos);
    $tag = substr ($tag, strlen($start));

    return $tag;

    }





    // Open and read xml file. You can replace this with your xml data.


    $pos = 0;
    $Nodes = array();

    if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
    }
    while ($getline = fread($fp, 4096)) {
    $data = $data . $getline;






    }

    $count = 0;
    $pos = 0;
    $timer2="0";

    // Goes throw XML file and creates an array of all <XML_TAG> tags.

    while ($node = GetElementByName($data, "<article>", "</article>")) {

    $Nodes[$count] = $node;
    $count++;
    $data = substr($data, $pos);
    }

    // Gets infomation from tag siblings.
    for ($i=0; $i<$count; $i++) {

    $title = GetElementByName($Nodes[$i], "<url>", "</url>");

    echo "bla $title <br>";

    }
    [/PHP]



    PHP Code:
    piece of xml file
       
    <gc>
          <
    article id="_0"
             <
    url>[url]http://www.x.com[/url]</url>

    ....... 
    Last edited by getbusy; 10-06-2004 at 03:38 PM.

  2. #2
    Join Date
    Jul 2004
    Posts
    38
    without looking thru your code in detail, all i can suggest is to look into using $attrs["ATTRIBUTENAME"], where "ATTRIBUTENAME" is obviously the name of the attribute you're looking for, in all caps. that should give you access to all the attributes of the current tag that is being read.

    good luck!

  3. #3
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Is there any reason that you aren't using the built-in XML parser?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •