We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Create XML file from a html file using php..

Has anyoane done this?I'm new with php and in need for help.
Please help.

3
Contributors
6
Replies
1 Month
Discussion Span
3 Months Ago
Last Updated
14
Views
Question
Answered
danimischiu
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi,

First, you will have provide us with the tags for your xml files. What I meant by tags is your own mark up based on your needs or how do you want the xml file structured.

something like this,

<?xml version="1.0"?>
<mydata>
<item>
<title>my title</title>
<description>my description</description>
</item>
</mydata>

Second, you will have to provide us with your sample html document, so that we are able to see if it can be parsed using PHP, and then ultimately write the parsed data in xml format as file or directly to the browser.

example of possible HTML doc..

<html>
<head>
</head>
<body>
<h1> Title </h1>
<p> My description</p>
</body>
</html>
veedeoo
Master Poster
765 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

Veedeoo the xml structure should be like the one you proposed but with a root node named company.
I've managed to get an array from the html.My problems is now etracting the data that I need to put in the xml file.My problem now is that for every item in the array for example:

` [1]=>
  string(371) "<tr align="left">
                    <td width="345" bgcolor="#fcf8f5" ><font size="2" color="#0B0B0B"
                        face="Times New Roman">**Adress:**</font></td>
                    <td align="center" width="330" bgcolor="#fcf8f5" ><font size="2"
                        face="Times New Roman">
                        **1042 Forest,Budapest**          
                    </font></td>
                </tr>"`

the data ("Adress") should be my items title and ("1042 Forest,Budapest") should be items description,and I now I need to extract that data somehow...

danimischiu
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Example of XML structure,this is how I want to make it look:

<company>
<cif>17982295</cif>
<address>Str. Tulcea 24 Cluj</address>
<city>Napoca</city>
<fax/>
<name>B It Company Srl</name>
<phone>0728898111</phone>
<registration_id>J12 /3408 /2005</registration_id>
<authorization_number/>
<state>Cluj</state>
<vat>0</vat>
<zip>400594</zip>
<created_at type="datetime">2013-01-29T12:18:34+00:00</created_at>
<updated_at type="datetime">2013-01-29T12:18:34+00:00</updated_at>
<consecutive_update_fails type="integer"/>
</company>

so..is there any way to get the data that I need from that array??

danimischiu
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi,

Sorry for late response... If will be difficult to parse html by converting them into array like that and then re-parse for the xml writter script.

What we need to do is use a dom html parser..download this file from sourceforge and test it with simple html first..,

follow this instruction here..

for example, if we have a simple html document like this

 <html>
 <head>
 <title>this is going to be parsed</title>
 </head>
 <body>
 <table>
 <tr>
 <td>address 1</td>
 <td>address 2</td>
 <td>address 3</td>
 </tr>
 </table>
 </body>

The sample PHP for the parser can be like this..

include_once 'lOCATION_OF_SIMPLE_HTML_DOM_CLASS';
$html = file_get_html('htmllocation');

function parse_this($html){
$td_data = array();
foreach($html->find('tr') as $row){
   foreach($row->find('td') as $item){
    $td_data[] = $item;

}
}
## address 1 to 3 are in this array

return $$td_data;

}

## test it
print_r (parse_this($html));

Let me know if you successfully print out the array as shown in my example... Once you got it working, it will be so easy to generate an xml file..

veedeoo
Master Poster
765 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

you could use the header('Content-type: text/xml'); then try to print a xml tags
like the example below as it get data from the table the data will be displayed as xml

              $stmt = "select * from table";
              $query = mysql_query($stmt);

              $data = array();
                while($row=mssql_fetch_assoc($query)){
                    $data[] = array($_GET['query'] => $row);
                }
                echo generate_response($data);

function generate_response($data){
    $retval = header('Content-type: text/xml');
    $retval .='<data>';
    if(is_array($data)){
        foreach($data as $val => $value){
            if(is_array($value)){
                foreach($value as $key => $cont){
                    if(is_array($cont)){
                        $retval .= '<'.$key.'>';
                        foreach($cont as $tag => $htm){
                            $retval.='<'.$tag.'>'.$htm.'</'.$tag.'>';
                        }
                        $retval .='</'.$key.'>';
                    }
                }   
            }       
        }
    }
    $retval .= '</data>';
    return $retval;
}
code739
Posting Whiz in Training
210 posts since May 2012
Reputation Points: 17
Solved Threads: 28
Skill Endorsements: 5

Thank you all for support.I've managed to make it work.

danimischiu
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by veedeoo and code739

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0760 seconds using 2.68MB