Edit XML File and Save it from PHP - php

I need to edit this XML file and update the values, and save to existing XML file.
<resources>
<string name="application">generation</string>
<string name="application2">generation2</string>
<string-type name="type">single</string-type>
</resources>
I need to update the values, 'generation', 'generation2', 'single' from db.
Thanks... hope a quick response. Thanks again.
How to delete child nods??!

You should look into DOMDocument which can parse XML or HTML, modify it and export the modified version back to a string.

Related

Adding a comment with SimpleDOM using xpath

I'm trying to add a comment in a xml file to a specific element, every element has it's unique names. My file is a translation file so it only contains string-elements and plurals etc.
Here's the snippet I use for my first attempt to add a comment:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="debug">You wish you could use that!</string>
<string name="author">foobar</string> <!-- Insert author name here -->
<string name="error">Wutfuq</string> <!-- New! -->
</resources>
And here my php file where I try to add a comment to the element with the name "debug":
<?php
error_reporting(E_ALL);
include "SimpleDOM.php";
$xml = simpledom_load_file("strings.xml");
$xml->xpath("//string[#name='debug']")->insertComment(' This is a test comment ', 'after');
$xml -> asXML('test.xml');
echo "This line should be shown, otherwise there might be some error";
?>
So my problem is that this won't work.
The whole page is white, there is no error and my test.xml won't be created, so the mistake seems to be at the line where I'm trying to add the comment while using xpath.
I'd be grateful for help and please don't try to convince me to use DOM.
According to the PHP doc, SimpleXMLElement::xpath() returns an array. You can't chain it with insertComment().
EDIT :
You can use isertComment with SimpleDom but only on the value :
$result = $xml->xpath("//string[#name='debug']")
$result[0]->insertComment(' This is a test comment ', 'after'); // after, append or before
$xml->asXML('test.xml');

Xml Category on product feed

i want to post products to amazon to category of "SportsMemorabilia", using the mws product feed.
i looked over the documentation, it is hard to understand, i also download the xsd to see the xml structure
but still it dosent working.
for this xml is sent (it is only the category part, i also sending all other elements of the product xml..)
<ProductData>
<SportsMemorabilia>
<ProductType>SportsMemorabilia</ProductType>
</SportsMemorabilia>
</ProductData>
it give me this error
<Result>
<MessageID>0</MessageID>
<ResultCode>Error</ResultCode>
<ResultMessageCode>5000</ResultMessageCode>
<ResultDescription>XML Parsing Error at Line 104, Column 26: cvc-complex-type.2.4.b: The content of element 'SportsMemorabilia' is not complete. One of '{AuthenticatedBy}' is expected.</ResultDescription>
</Result>
please, write to me how the xml should look like in order it to work,
thank you.
As per the current version, this is the minimum XML that should pass the published XSD. It doesn't mean it'll work, as functionality built in the service may do additional validation.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<SportsMemorabilia>
<ProductType>SportsMemorabilia</ProductType>
<AuthenticatedBy>AuthenticatedBy1</AuthenticatedBy>
<ConditionProvidedBy>ConditionProvidedBy1</ConditionProvidedBy>
<ConditionRating>ConditionRating1</ConditionRating>
</SportsMemorabilia>
To access the services you need to have an account setup; you have to also fill in the other two elements, at least.
I would strongly suggest you to walk through the document posted here... At least the minimum above should get you over XSD validation errors.

SimpleXML and PHP (XPATH)

Ok guys I'm using xml to store page information for a dynamic website. I need to figure out a way to take a variable(file name) and pull the related node and parse that information. so basically here's my XML structure...
<SITE>
<PAGE>
<FILENAME>sub.php</FILENAME>
<DESCRIPTION>this is an example sub page</DESCRIPTION>
<TITLE>NOH Sub page</TITLE>
<PARENTS>
<PARENT>What is NOH</PARENT>
</PARENTS>
</PAGE>
<PAGE>
<FILENAME>about.php</FILENAME>
<DESCRIPTION>description description description </DESCRIPTION>
<TITLE>About Us</TITLE>
<PARENTS>
<PARENT>Company</PARENT>
<PARENT>People</PARENT>
</PARENTS>
</PAGE>
</SITE>
Is there a way to use SimpleXML and PHP to take a variable say.. 'sub.php' and say I want the node where filename = 'sub.php', and then be able to parse that node(page) for needed info. Thanks!
Update, where I'm confused is...
i have this function
function getPage($pagePath){
$file = "includes/sitestructure.xml";
$xml = simplexml_load_file($file);
return print_r($xml-xpath(PAGE/FILENAME));
}
but my xpath doesn't work, and I'm not sure how to logically get the info I need.. I know how to normally parse XML with PHP but not how to specify a specific node by comparing it to a variable then parse it.
$xml-xpath(PAGE/FILENAME)
This is broken in several ways, but this is an XPath question not a PHP syntax one.
You want to get the PAGE element(s) which contain a FILENAME of your chosen file name. To do that in XPath, a predicate can be used.
PAGE[FILENAME="sub.php"]
In your PHP code that might look like
$pages = $xml->xpath('PAGE[FILENAME="sub.php"]');
$first_page = $pages[0];
echo $first_page->TITLE; // NOH Sub page
The important point here is the use of the predicate to filter the result to only the page element(s) that you want.
http://www.w3.org/TR/xpath/#predicates

PHP XML DOM getElementById

I need to pick one tag from xml file and insert another tag before this tag. I'm doing this with method insertBefore in DOM, but the problem is, that if I want pick the tag before I want to add the another tag by method getElementById, it doesn't work.
It writes "using non-object". This is how the tag looks:
<item id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>
Somewhere I read that it must look like this, but I can't edit all files:
<item xml:id="Flow_0" href="Flow_0.html" media-type="application/xhtml+xml"/>
Have you got any idea how to do that?
A common workaround is to use XPath to get the element.
$item = $xpath->query('//item[#id="Flow_0"]')->item(0);

How to update XML file with ajax?

I have xml storage with in this format
<Contacts>
<Contact>
<![CDATA["Some HTML"]]>
</Contact>
<Contact>
<![CDATA["Some HTML"]]>
</Contact>
</Contacts>
I am using XMLHttpRequest to read the data and put it inside a "div" on the page. Now I make some changes on it via JavaScript and I would like to know how can I update the changes made back to the XML file from where I took the data.
I've been googling a lot but I have problems understanding those forums because they are not describing examples similar like mine.
Try using a ajax call which you give the xml data. You can then save the data using simplexml http://nl.php.net/manual/en/book.simplexml.php or using http://nl.php.net/manual/en/book.domxml.php
Leave the "retrieving" your XML on JavaScript, and "saving changes" on PHP. Using jQuery you just $.get() your XML file, and when you save it (let it be .click, .live('click') etc.)
you $.post() strings you wrote in some input to something like save_xml.php. There are some tools for working with XML files in PHP. If you get on well with Smarty, I advise you to keep sort of my_xml_template.tpl , which after smarty->fetch you save in a file with file_put_contents(). Cheers.

Categories