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');
Related
I have a Custom Tag Generator system for XML. I am using custom PHP. Here I am able to add custom XML tags and add its functionality. What I want to achieve is that I would like to show display contents from a specific tag only. Check out the following code if mine:
<?xml version="1.0" encoding="UTF-8"?>
<jsaonthego>
<jobdetails>
<templateversion>1</templateversion>
<title>Testing Title</title>
<taskdetails>Testing Details</taskdetails>
</jobdetails>
<cem>
<item>999</item>
</cem>
</jsaonthego>
From the above code I would like to show "Testing Title" only but not anything else.
So far I am using the following code without having any luck.
<?php
$result = preg_replace('/<jsaonthego<jobdetails<templateversion<taskdetails<cem<item\b[^>]*>(.*?)<\/jsaonthego>item>taskdetails>templateversion>jobdetails>cem>/i', '', $title);
echo $result;
?>
Thanks in advance.
PHP has several builtin XML libraries. For this use case I would opt for SimpleXML:
$jsaonthego = simplexml_load_string($xml);
echo $jsaonthego->jobdetails->title;
(demo)
How to extract in php this type of XML format? i try 'simplexml_load_file' function but its not work, it work only normal xml format.
Thanks in advance :)
<?xml version="1.0"?>
<a:p>
<a:r>
<a:rPr strike="noStrike" u="none" b="0" cap="none" baseline="0" sz="1400" lang="en-US" i="0">
<a:solidFill>
<a:srgbClr val="595959"/>
</a:solidFill>
<a:latin typeface="Arial"/>
<a:ea typeface="Arial"/>
<a:cs typeface="Arial"/>
<a:sym typeface="Arial"/>
</a:rPr>
<a:t>E-mail Address</a:t>
</a:r>
The XML you show isn't well-formed, meaning it doesn't match the structure of what XML should look like. Specifically, the <a:p> element on line 2 doesn't have an ending tag, </a:p>.
Adding that ending tag should make simplexml_load_string work, but you'll also get warnings because of having an undefined namespace, a. The a: that's part of each element name is saying those elements are part of a namespace whose alias is a. To fix this, you would add an attribute to your root element to define that alias, like this: <a:p xmlns:a="some-namespace"> (replace some-namespace with the actual namespace for the XML content you're using, obviously--this looks like it could be the content of an MS Word Document, so the namespace might be something like http://schemas.microsoft.com/office/word/2003/wordml, as a guess).
Once you start using the namespace correctly, though, you'll have to inform simplexml_load_string that the content you're loading is in that namespace; this is done via the fourth argument to the function, ns.
A complete, working example is:
<?php
$content = <<<XML
<?xml version="1.0"?>
<a:p xmlns:a="some-namespace">
<a:r>
<a:rPr strike="noStrike" u="none" b="0" cap="none" baseline="0" sz="1400" lang="en-US" i="0">
<a:solidFill>
<a:srgbClr val="595959"/>
</a:solidFill>
<a:latin typeface="Arial"/>
<a:ea typeface="Arial"/>
<a:cs typeface="Arial"/>
<a:sym typeface="Arial"/>
</a:rPr>
<a:t>E-mail Address</a:t>
</a:r>
</a:p>
XML;
$xml = simplexml_load_string($content, "SimpleXMLElement", 0, "some-namespace");
print_r($xml);
?>
Obviously, you'd normally be reading the XML from a file or some such, but I included it inline in a heredoc for simplicity.
You can look at the simplexml_load_string and SimpleXMLElement documentation for additional helpful details.
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.
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
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);