Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
parsing and reading xml
Old 07-07-2009, 08:08 AM parsing and reading xml
rezzy's Avatar
Super Talker

Posts: 115
Location: in the interwebz
Trades: 0
Hello all,

I am working on making a small CMS for a personal project.The cms will use a simple xml document with the following format.
Code:
<?xml version="1.0"?>

<root>
<page><id>1</id>
    <title>Sample Date</title>
    <description>This is the description</description>
    <content>This is some content</content>
</page>

<page> <id>2</id>
    <title>Sample Content 2</title>
    <description>This is the description</description>
    <content>This is some content</content>
</page>

</root>
At this point, I can get information, but I am unable to choose which piece I want. My thought was to find the node from the start tag, add a marker and send that to the actual node.

Somehow, I think this logic is flawed. I would like to be able to search the document by id, and then work on displaying the content contained under that id.
PHP Code:
$file "database.xml";

function 
defaultHandler($parser$text){
echo 
$text .'default';

}
function 
startElement($parser$name$text){
    
$val '2';

    
$name strtolower($name);
    if (
$name == 'id')
    {
    
    
$x "1";
    print 
$x;
    return 
$x;
    
    }
     else
       
$x "0";
       print 
$x;
       return 
$x;
       
       
}


         

function 
text ($parser$text$x){
print 
$x;
 if(
preg_match("/\S/"$text)) {
     
 }
}


function 
endElement($parser$name){

}


$xml_parser_object xml_parser_create();
xml_set_element_handler($xml_parser_object"startElement""endElement");
xml_set_character_data_handler($xml_parser_object"text");
xml_set_default_handler($xml_parser_object"defaultHandler");

if (!
$handle fopen($file"r")) {
    die(
"Error.");
}
while (
$xml_data fread($handle1024)) {
    if (!
xml_parse($xml_parser_object$xml_datafeof($handle))){
        die(
sprintf("Error %s on line %d"
                
xml_error_string(xml_get_error_code($xml_parser_object)), xml_get_current_line_number($xml_parser_object)));
    }
}

xml_parser_free($xml_parser_object);
fclose($handle); 
__________________

Please login or register to view this content. Registration is FREE

Resnodesigns
rezzy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-08-2009, 01:20 AM Re: parsing and reading xml
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Why don't you just use simplexml?

Construct your XML data like this:
Code:
<?xml version="1.0"?>
<root>
    <page id="1">
        <title>Sample Date</title>
        <description>This is the description</description>
        <content>This is some content</content>
    </page>
    <page id="2">
        <title>Sample Content 2</title>
        <description>This is the description</description>
        <content>This is some content</content>
    </page>
</root>
And your php is simple with a little function to search data:
PHP Code:
<?php
    
  
if (!$xml_content file_get_contents('xml.xml')) die('Unable to get XML file');
  
  if (
$xml simplexml_load_string($xml_content))
  {
    echo 
get_xml_node_value_from_attribute('title''id''2'$xml);
  }
  
  
// Define locate function
  
function get_xml_node_value_from_attribute($node$attribute$value$xml)
  {
    
$string '';
    
    foreach (
$xml AS $element)
    {
      if (
$element[$attribute] == $value)
      {
        
$string .= (string)$element->$node;
      }
    }
    
    return 
$string;
  }
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-10-2009, 12:07 AM Re: parsing and reading xml
rezzy's Avatar
Super Talker

Posts: 115
Location: in the interwebz
Trades: 0
Never mind, I figured out what was confusing me. I feel like such an idiot LOL. Thanks.
__________________

Please login or register to view this content. Registration is FREE

Resnodesigns

Last edited by rezzy; 07-10-2009 at 12:12 AM..
rezzy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to parsing and reading xml
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59322 seconds with 12 queries