Append XML File using PHP

akv003

New Member
Messages
23
Reaction score
0
Points
0
Hi,

I am naive as far as php programming is concern but very good at xml. Can some one tell me how to append XML file using Php code

thanks in advance.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
What do you mean by append? Append it to another file? Append it to output? Append it to more xml? I'm sure I or someone else here can help, but you need to be more descriptive about exactly what you want done.
 

akv003

New Member
Messages
23
Reaction score
0
Points
0
Sorry for not providing more detail.

I need to modify an existing XML file by appending record in it. My application goes like this, I need to design a product submission page and when I submit it should be stored into XML file and again if I submit another product detail it should append to above product record. Finally, I need to display XML file records on webpage (This piece I can handled using XSLT).

let me know, if you need more detail

thanks
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
waht you would do is use the php fwrite method in order to write the file. I am not very sure of how to do this, but I know it can be done.
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
A MySQL database would be much more appropriate for this. MySQL allows you to update, add and remove rows easily, much more easily than using XML and fwrite to do it. Look into it, SQL isn't hard to learn.

-Luke.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
hmmm. I never though of that. And SQL is kinda like XML anyways...not really...but kinda.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
One thing you guys are missing is that he needs the xml document to generate the page with xslt. However, I do agree that the data should be stored in a database for easier manipulation. Also, those xml functions don't appear to be supported by x10. XML parsing and the XMLWriter object are supported, but to do something like this they're a bit unwieldy.

Personally, I would say that given these circumstances you should store all the data in the db and regenerate the xml document whenever it changes. However, this may require changing a lot of things with your current system. So for now I'll just give you a rather simple function to accomplish what you want:

PHP:
function append_xml($file, $content, $sibling, $single = false) {
    $doc = file_get_contents($file);
    if ($single) {
        $pos = strrpos($doc, "<$sibling");
        $pos = strpos($doc, ">", $pos) + 1;
    }
    else {
        $pos = strrpos($doc, "</$sibling>") + strlen("</$sibling>");
    }
    return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
}
This function works by taking the name/path of an xml document, the xml you want written to it, and the element it should be added after. So if you had an xml document named products.xml and it looked like this:

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

<products>
    <product>
        <id>1</id>
        <name>Product 1</name>
        <price>21.00</price>
    </product>
    <product>
        <id>2</id>
        <name>Product 2</name>
        <price>42.00</price>
    </product>
</product>
You could add another product element with this code (provided that the document is stored in the same directory as the script):

PHP:
$content = '<product><id>3</id><name>Product 3</name><price>63.00</price></product>';
append_xml('products.xml', $content, 'product');
The last parameter in the function, $single, should be set to true if you're appending the content after a single-tag element(e.g., <element />). For example if your xml document looked like this instead:

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

<products>
    <product id="1" name="Product 1" price="21.00" />
    <product id="2" name="Product 2" price="42.00" />
</products>
You would change the function call to this:

PHP:
append_xml('products.xml', $content, 'product', true);
 

akv003

New Member
Messages
23
Reaction score
0
Points
0
Thanks friends for suggestion.

I would go with woiwky solution. I liked this approach
I will keep this thread open for few more days, so If someone interested to provide more detail on XML-PHP. (Guys no more DB suggestion please).

One thing you guys are missing is that he needs the xml document to generate the page with xslt. However, I do agree that the data should be stored in a database for easier manipulation. Also, those xml functions don't appear to be supported by x10. XML parsing and the XMLWriter object are supported, but to do something like this they're a bit unwieldy.

Personally, I would say that given these circumstances you should store all the data in the db and regenerate the xml document whenever it changes. However, this may require changing a lot of things with your current system. So for now I'll just give you a rather simple function to accomplish what you want:

PHP:
function append_xml($file, $content, $sibling, $single = false) {
    $doc = file_get_contents($file);
    if ($single) {
        $pos = strrpos($doc, "<$sibling");
        $pos = strpos($doc, ">", $pos) + 1;
    }
    else {
        $pos = strrpos($doc, "</$sibling>") + strlen("</$sibling>");
    }
    return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
}
This function works by taking the name/path of an xml document, the xml you want written to it, and the element it should be added after. So if you had an xml document named products.xml and it looked like this:

Code:
<?xml version="1.0" encoding="UTF-8" ?>
 
<products>
    <product>
        <id>1</id>
        <name>Product 1</name>
        <price>21.00</price>
    </product>
    <product>
        <id>2</id>
        <name>Product 2</name>
        <price>42.00</price>
    </product>
</product>
You could add another product element with this code (provided that the document is stored in the same directory as the script):

PHP:
$content = '<product><id>3</id><name>Product 3</name><price>63.00</price></product>';
append_xml('products.xml', $content, 'product');
The last parameter in the function, $single, should be set to true if you're appending the content after a single-tag element(e.g., <element />). For example if your xml document looked like this instead:

Code:
<?xml version="1.0" encoding="UTF-8" ?>
 
<products>
    <product id="1" name="Product 1" price="21.00" />
    <product id="2" name="Product 2" price="42.00" />
</products>
You would change the function call to this:

PHP:
append_xml('products.xml', $content, 'product', true);

Solution Accepted.
 
Last edited by a moderator:

stalkio

New Member
Messages
45
Reaction score
0
Points
0
Interesting... - I tried to access some live feeds and was given the following message:

MM_XSLTransform error.

The server could not perform the XSL transformation because an XSLT processor for PHP could not be found. Contact your server administrator and ask them to install an XSLT processor for PHP.

So this is as woiwky suggests = that it is not supported by X10... CAN we install it or is there anyway around this??
 
Top