I am trying to add the information below into a Wordpress database table (specifically the wp_postmeta table). But I can't figure out how to add the ones with the same node name.

The XML document is:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.1/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.1/"
>

<channel>
    <title>Title</title>
    <link>http://localhost/WP/</link>
    <description>Just another WordPress site</description>
    <pubDate>Mon, 30 Jul 2012 08:53:47 +0000</pubDate>
    <language>en</language>
    <wp:wxr_version>1.1</wp:wxr_version>
    <wp:base_site_url>http://localhost/WP/</wp:base_site_url>
    <wp:base_blog_url>http://localhost/WP/</wp:base_blog_url>
    <generator>http://wordpress.org/?v=3.3.2</generator>
    <item>
        <title>IMPORT TEST 1</title> 
        <link>http://localhost/WP/IMPORT TEST/</link>
        <dc:creator>admin</dc:creator>
        <wp:comment_status>open</wp:comment_status>
        <wp:status>publish</wp:status>
        <wp:post_type>post</wp:post_type>
        <category domain="category" nicename="plumbing"><![CDATA[Plumbing]]></category> 
        <category domain="category" nicename="news"><![CDATA[NEWS]]></category>    
        <wp:postmeta>
        <wp:meta_key>thumbnail</wp:meta_key>
            <wp:meta_value><![CDATA[decorating8.jpg]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>content</wp:meta_key>                            
            <wp:meta_value><![CDATA[THIS PART IS TO BE FILLED WITH THE TEXT YOU WANT]]></wp:meta_value>  
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>youtube_video_link</wp:meta_key>
            <wp:meta_value><![CDATA[]]></wp:meta_value> 
        </wp:postmeta>
    </item>
</channel>
</rss>

The code I have so far is:

<?php
//error display
error_reporting(E_ALL);
ini_set('display_errors', true);

$target_path = "temp/";

$target_path = $target_path . basename($_FILES['feed']['name']); 

if(move_uploaded_file($_FILES['feed']['tmp_name'], $target_path)) {

    $time = time();
    $rand = rand(100,500);
    rename("temp/".basename($_FILES['feed']['name']), "temp/".$time.$rand.".xml");

    $filename = $time.$rand.".xml";

    $objDOM = new DOMDocument();
    $objDOM->load("temp/".$filename);
    $job = $objDOM->getElementsByTagName("job"); 

    foreach( $job as $value ) {
    $title = $value->getElementsByTagName("title");
    $link = $value->getElementsByTagName("link");
    $post_name = $value->getElementsByTagName("post_name");
    $status = $value->getElementsByTagName("status");
    $category = $value->getElementsByTagName("category");
    $category = $value->getElementsByTagName("category");
    $wp:postmeta = $value->getElementsByTagName("wp:postmeta");
    $wp:postmeta = $value->getElementsByTagName("wp:postmeta");
    $wp:postmeta = $value->getElementsByTagName("wp:postmeta");
    $wp:postmeta = $value->getElementsByTagName("wp:postmeta");
    $wp:postmeta = $value->getElementsByTagName("wp:postmeta");
    }

    $f01 = $title->item(0)->nodeValue;
    $f02  = $link->item(0)->nodeValue;
    $f03  = $post_name->item(0)->nodeValue;
    $f04  = $status->item(0)->nodeValue;
    $f05  = $category->item(0)->nodeValue;
    $f06  = $wp:postmeta->item(0)->nodeValue;
    $f07 = $wp:

    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());

//if insert entry   
if($title != ""){

    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value )" ) or die(mysql_error());
    $current_id = mysql_insert_id(); 
} else{
    //file didn't been upload properly
    echo "There was an error uploading the file, please try again!";
}

?>

$wp:postmeta = $value->getElementsByTagName("wp:postmeta");

You should handle this just like you did with "job", with a foreach.

Can you be more specific? Is it something like this?

<?php
    //error display
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    $target_path = "temp/";
    $target_path = $target_path . basename($_FILES['feed']['name']);
    if(move_uploaded_file($_FILES['feed']['tmp_name'], $target_path)) {
    $time = time();
    $rand = rand(100,500);
    rename("temp/".basename($_FILES['feed']['name']), "temp/".$time.$rand.".xml");
    $filename = $time.$rand.".xml";
    $objDOM = new DOMDocument();
    $objDOM->load("temp/".$filename);
    $job = $objDOM->getElementsByTagName("job");

    foreach( $job as $value ) {
        $meta_value = $value->getElementsByTagName("title");
        $link = $value->getElementsByTagName("link");
        $post_name = $value->getElementsByTagName("post_name");
        $status = $value->getElementsByTagName("status");
        $category = $value->getElementsByTagName("category");
        $category = $value->getElementsByTagName("category");


        $f01 = $title->item(0)->nodeValue;
        $f02  = $link->item(0)->nodeValue;
        $f03  = $post_name->item(0)->nodeValue;
        $f04  = $status->item(0)->nodeValue;
        $f05  = $category->item(0)->nodeValue;


        foreach( $post as $entry ) {
            $meta_key0.$entry = $wp:postmeta = $entry->getElementsByTagName("wp:meta_key");
            $meta_value0.$entry = $wp:postmeta = $entry->getElementsByTagName("wp:meta_value");
            $meta_key0.$entry  = $wp:postmeta->item(0)->nodeValue;
            $meta_value0.$entry  = $wp:postmeta->item(0)->nodeValue;
        }
    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());
    //if insert entry
    if($title != ""){
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value )" ) or die(mysql_error());
    $current_id = mysql_insert_id();
    } else{
    //file didn't been upload properly
    echo "There was an error uploading the file, please try again!";
    }
    ?>

Thanks

I have just written this, but it isn't doing anything. What is wrong with the code?

<?php
//error display
error_reporting(E_ALL);
ini_set('display_errors', true);

$target_path = "temp/";

$target_path = $target_path . basename($_FILES['feed']['name']); 

if(move_uploaded_file($_FILES['feed']['tmp_name'], $target_path)) {

    $time = time();
    $rand = rand(100,500);
    rename("temp/".basename($_FILES['feed']['name']), "temp/".$time.$rand.".xml");

    $filename = $time.$rand.".xml";

    $objDOM = new DOMDocument();
    $objDOM->load("temp/".$filename);
    $job = $objDOM->getElementsByTagName("job"); 

    foreach( $job as $value ) {

    //Get the value of the elements
    $title = $value->getElementsByTagName("title");
    $link = $value->getElementsByTagName("link");
    $post_name = $value->getElementsByTagName("post_name");
    $status = $value->getElementsByTagName("status");
    $category = $value->getElementsByTagName("category");
    $category = $value->getElementsByTagName("category");

    //Establish connection
    mysql_connect("localhost", "user", "pass") or die(mysql_error());
    mysql_select_db("ableskillswp") or die(mysql_error());


    //Assign values and insert into wp_postmeta table
    $meta_value = $title->item(0)->nodeValue;
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'title','$meta_value' )" ) or die(mysql_error());

    $meta_value  = $link->item(0)->nodeValue;
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'link','$meta_value' )" ) or die(mysql_error());

    $meta_value  = $post_name->item(0)->nodeValue;
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'post_name','$meta_value' )" ) or die(mysql_error());

    $meta_value  = $status->item(0)->nodeValue;
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'status','$meta_value' )" ) or die(mysql_error());

    $meta_value  = $category->item(0)->nodeValue;
    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'category','$meta_value' )" ) or die(mysql_error());

    $post = $objDOM->getElementsByTagName("wp:postmeta");

        //Loop through the postmeta tags and add to table
        foreach( $post as $entry ) {
            $meta_key = $wp:postmeta = $entry->getElementsByTagName("wp:meta_key");
            $meta_value = $wp:postmeta = $entry->getElementsByTagName("wp:meta_value");

            $meta_key = $wp:postmeta->item(0)->nodeValue;
            $meta_value  = $wp:postmeta->item(0)->nodeValue;

            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( '$meta_key','$meta_value' )" ) or die(mysql_error());
        }

    $current_id = mysql_insert_id(); 

    else{
    //file didn't been upload properly
    echo "There was an error uploading the file, please try again!";
}

};

?>

I have just written this, but it isn't doing anything. What is wrong with the code?

Getting any errors ? If not, add output at key places to determine where the code stops.

I've made some fixes. Everything works as expected, but at the "//Loop through the postmeta tags and add to table" point, it doesn't work. This section is not executing and nothing is getting added to the table.

<?php

//error display
error_reporting(E_ALL);
ini_set('display_errors', 1);    

$target_path = "temp/";

$target_path = $target_path . basename($_FILES['feed']['name']); 

if(move_uploaded_file($_FILES['feed']['tmp_name'], $target_path)) {

    $time = time();
    $rand = rand(100,500);
    rename("temp/".basename($_FILES['feed']['name']), "temp/".$time.$rand.".xml");

    $filename = $time.$rand.".xml";

    $objDOM = new DOMDocument();
    $objDOM->load("temp/".$filename);
    $item = $objDOM->getElementsByTagName("item"); 

    foreach( $item as $value ) {
        echo 'hello';
        //Get the value of the elements
        $title = $value->getElementsByTagName("title");


        $link = $value->getElementsByTagName("link");
        $post_name = $value->getElementsByTagName("post_name");
        $status = $value->getElementsByTagName("status");
        $category = $value->getElementsByTagName("category");
        $category = $value->getElementsByTagName("category");


        //Establish connection
        mysql_connect("localhost", "root", "password") or die(mysql_error());
        mysql_select_db("dbname") or die(mysql_error());


        //Assign values and insert into wp_postmeta table
        $meta_value = $title->item(0)->nodeValue;
        $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'title','$meta_value' )" ) or die(mysql_error());

        $meta_value  = $link->item(0)->nodeValue;
        $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'link','$meta_value' )" ) or die(mysql_error());

        $meta_value  = $post_name->item(0)->nodeValue;
        $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'post_name','$meta_value' )" ) or die(mysql_error());

        $meta_value  = $status->item(0)->nodeValue;
        $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'status','$meta_value' )" ) or die(mysql_error());

        $meta_value  = $category->item(0)->nodeValue;
        $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'category','$meta_value' )" ) or die(mysql_error());

        $post = $objDOM->getElementsByTagName("wp:postmeta");

            //Loop through the postmeta tags and add to table
            foreach( $post as $entry ) {
                echo 'in the loop';

                    $meta_key = $value->getElementsByTagName("wp:meta_key");
                    $meta_value = $value->getElementsByTagName("wp:meta_value");    

                    $meta_key  = $meta_key->item(0)->nodeValue;         
                    $meta_value  = $meta_value->item(0)->nodeValue;             

                $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( '$meta_key','$meta_value' )" ) or die(mysql_error());
            }

        $current_id = mysql_insert_id(); 

    }

}

?>

$post = $objDOM->getElementsByTagName("wp:postmeta");

Am not entirely sure, but for this to work I think the wp namespace needs to be added to the DOMDocument.

I've made some fixes. I've managed to add everything as I want to the database. But I expected the change in the wp_postmeta table to be reflected in the wp admin area. Guess I haven't added everything necessary for this to happen. If anyone knows about wordpress and what alterations are necessary on any of the tables in order for a post to appear under its post tab please let me know.

This is giving me error.Fatal error: Call to a member function item() on a non-object in C:\websites\WP\jobs\upload.php on line 53

What is wrong with it.

<?php

    //error display
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $target_path = "temp/";

    $target_path = $target_path . basename($_FILES['feed']['name']); 

    if(move_uploaded_file($_FILES['feed']['tmp_name'], $target_path)) {

        $time = time();
        $rand = rand(100,500);
        rename("temp/".basename($_FILES['feed']['name']), "temp/".$time.$rand.".xml");

        $filename = $time.$rand.".xml";

        $objDOM = new DOMDocument();
        $objDOM->load("temp/".$filename);
        $item = $objDOM->getElementsByTagName("item"); 

        foreach( $item as $value ) {

            //Get the value of the elements
            $content_encoded = $value->getElementsByTagName("content_encoded");
            $excerpt_encoded = $value->getElementsByTagName("excerpt_encoded");
            $creator = $value->getElementsByTagName("creator");
            $title = $value->getElementsByTagName("title");
            $link = $value->getElementsByTagName("link");
            $post_name = $value->getElementsByTagName("post_name");
            $status = $value->getElementsByTagName("status");
            $category1 = $value->getElementsByTagName("category1");
            $category2 = $value->getElementsByTagName("category2");

            //Establish connection
            mysql_connect("localhost", "root", "password") or die(mysql_error());
            mysql_select_db("dbname") or die(mysql_error());

            //Assign values and insert into wp_postmeta table
            $creator = $creator->item(0)->nodeValue;
            $title = $title->item(0)->nodeValue;
            $excerpt_encoded = $excerpt_encoded->item(0)->nodeValue;
            $status = $status->item(0)->nodeValue;
            $content_encoded = $content_encoded->item(0)->nodeValue;



            $query = mysql_query("INSERT INTO wp_posts (post_author, post_title, post_content,to_ping, post_excerpt, post_status, comment_status, post_type, pinged,post_content_filtered) VALUES ( '1','$title','','','$excerpt_encoded','$status','open','post','','' )" ) or die(mysql_error());

            $meta_value = $title->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'title','$meta_value' )" ) or die(mysql_error());

            $meta_value  = $link->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'link','$meta_value' )" ) or die(mysql_error());

            $meta_value  = $post_name->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'post_name','$meta_value' )" ) or die(mysql_error());

            $meta_value  = $status->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'status','$meta_value' )" ) or die(mysql_error());

            $meta_value  = $category1->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'category','$meta_value' )" ) or die(mysql_error());

            $meta_value  = $category2->item(0)->nodeValue;
            $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( 'category','$meta_value' )" ) or die(mysql_error());


            $element = $objDOM->getElementsByTagName("postmeta");

                //Loop through the postmeta tags and add to table
                foreach( $element as $entry ) {
                    echo 'in the loop';

                        $meta_key = $entry->getElementsByTagName("meta_key");
                        $meta_value = $entry->getElementsByTagName("meta_value");   

                        $meta_key  = $meta_key->item(0)->nodeValue;         
                        $meta_value  = $meta_value->item(0)->nodeValue;             

                    $query = mysql_query("INSERT INTO wp_postmeta (meta_key, meta_value) VALUES ( '$meta_key','$meta_value' )" ) or die(mysql_error());
                }

            $current_id = mysql_insert_id(); 

        }

    }

    ?>


XML document is giving below:

<?xml version="1.0" encoding="UTF-8" ?>

<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.1/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.1/"
>

<channel>
    <title>WP</title>
    <link>http://localhost/WP/</link>
    <description>Just another WordPress site</description>
    <pubDate>Mon, 30 Jul 2012 08:53:47 +0000</pubDate>
    <language>en</language>
    <wp:wxr_version>1.1</wp:wxr_version>
    <wp:base_site_url>http://localhost/WP/</wp:base_site_url>
    <wp:base_blog_url>http://localhost/WP/</wp:base_blog_url>
    <generator>http://wordpress.org/?v=3.3.2</generator>


    <item>
        <title>IMPORT TEST 3</title> 
        <link>http://localhost/WP/IMPORT TEST/</link>
        <creator>admin</creator>
        <content_encoded><![CDATA[]]></content_encoded>
        <excerpt_encoded><![CDATA[]]></excerpt_encoded>
        <comment_status>open</comment_status>
        <ping_status>open</ping_status>
        <post_name>Import Item 3</post_name> 
        <wp:status>publish</wp:status>
        <wp:post_type>post</wp:post_type>
        <category1 domain="category" nicename="plumbing"><![CDATA[Plumbing]]></category1> 
        <category2 domain="category" nicename="news"><![CDATA[NEWS]]></category2>  
        <postmeta>
            <meta_key>_edit_last</meta_key>
            <meta_value><![CDATA[1]]></meta_value>
        </postmeta>
        <postmeta>
            <meta_key>_mf_write_panel_id</meta_key>
            <meta_value><![CDATA[4]]></meta_value>
        </postmeta>

        <postmeta>
            <meta_key>thumbnail</meta_key>
            <meta_value><![CDATA[decorating8.jpg]]></meta_value>
        </postmeta>
        <postmeta>
            <meta_key>content</meta_key>                          
            <meta_value><![CDATA[THIS PART IS TO BE FILLED WITH THE TEXT YOU WANT]]></meta_value>  
        </postmeta>
        <postmeta>
            <meta_key>_wp_old_slug</meta_key>
            <meta_value><![CDATA[ENTER A SLUG FOR THE PAGE.]]></meta_value>
        </postmeta>
        <postmeta>
            <meta_key>image_1</meta_key>
            <meta_value><![CDATA[decorating8.jpg]]></meta_value>
        </postmeta>
        <postmeta>
            <meta_key>image_2</meta_key>
            <meta_value><![CDATA[decorating9.jpg]]></meta_value>
        </postmeta>
        <postmeta>
            <meta_key>youtube_video_link</meta_key>
            <meta_value><![CDATA[]]></meta_value> 
        </postmeta>
    </item>

</channel>
</rss>

->item(0)->nodeValue

One of these is executed, but the variable is null (no value). You have no checks in place to validate the results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.