I am working with an XML document with the structure shown below and i am trying to add/remove image objects without having to treat the xml document as text. Each image object corresponds to a picture in the album.
<?xml version="1.0" encoding="UTF-8"?>
<gallery galleryTitle="Photo Album">
<image imageURL="../Photoalbum/artwork/eb98e388e14b11008d4ffb595b9ed3e6.jpg"
thumbURL="../Photoalbum/artwork/thumbs/tn_eb98e388e14b11008d4ffb595b9ed3e6.jpg"
linkURL="../Photoalbum/artwork/eb98e388e14b11008d4ffb595b9ed3e6.jpg"
linkTarget="_blank">
<title>A Title</title>
<caption>A caption</caption>
</image>
</gallery>
I have been able to access and print the xml file as a whole with the code below
if (file_exists('file.xml')) {
$xml = simplexml_load_file('file.xml');
print"<pre>";
print_r($xml);
}
but so far have not been able to access individual fields of the image object. The object that will be added/removed will be identified by a unique filename such as eb98e388e14b11008d4ffb595b9ed3e6.jpg so once this filename is found in the XML document, i want the whole image object removed. Similarly, i would like to be able to add a new image object with a new filename.
Let me know if my question is clear.
Use xpath() to select the <image>-node. xpath is like SQL for XML:
$image = $xml->xpath("/gallery/image[contains(#imageURL, $filename)]");
... and unset() to remove that node:
if (isset($image[0])) unset($image[0][0]);
see it working: https://eval.in/135145
use the search-function to see how to add a new <image> with SimpleXml.
Related
I have successfully generated an xml file using dom in php dynamically, but I am unsure how to put CSS link in it.
I need the php code to link CSS with that automatically generated XML file.
To link an CSS in XML you can use the xml-stylesheet processing instruction. Use the corresponding method on DOMDocument to create it and then append/insert it just like any other node.
$document = new DOMDocument();
$document->appendChild(
$document->createProcessingInstruction(
'xml-stylesheet', 'type="text/css" href="styles.css"'
)
);
$document->appendChild(
$document->createElement('example')
);
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="styles.css"?>
<example/>
first time over here :o) ... need your help as this is driving me crazy this far away ... Right..
I have this xml structure:
<?xml version="1.0" encoding="UTF-8"?>
<juiceboxgallery galleryTitle="whatever">
<image imageURL="images/23.jpg" thumbURL="thumbs/23.jpg" linkURL="images/23.jpg" linkTarget="_blank" sourcePath="webadded"></image>
<image imageURL="images/24.jpg" thumbURL="thumbs/24.jpg" linkURL="images/24.jpg" linkTarget="_blank" sourcePath="webadded"></image>
... and so on ...
</juiceboxgallery>
And i want to insert new elements just before de first 'image' on the list...
What's the right way to do it..??
All my tries are resulting inserting just before 'juiceboxgallery' or after the closing tag. No way to put it where i want...
Trying to get the the first image element with getElemtnstByTagName("image")->item(0) and doing an insertBefore($new_donde,$first_node) throws me a Not Found Error!! with this code...
$nodo_zero=$doc->getElementsByTagName('image')->item(0);
$doc->insertBefore($nuevo_nodo,$nodo_zero);
.. even when cheking that I have 'X' image elements with this:
$images=$doc->getElementsByTagName("image");
echo "<hr>imagenes: ".$images->length."<hr>";
.. please some help :|
Methods like insertBefore() and appendChild() have to be called on the parent node. The document itself is a node, too. So it has the methods and allows to add multiple nodes, but only a single element node.
In your case, here are to possible approaches.
Use the DOMNode::parentNode property
$nodo_zero = $doc->getElementsByTagName('image')->item(0);
$nodo_zero->parentNode->insertBefore($nuevo_nodo,$nodo_zero);
Or fetch the juiceboxgallery node and use the DOMNode::firstChild property.
$gallery = $doc->getElementsByTagName('juiceboxgallery')->item(0);
$gallery->insertBefore($nuevo_nodo, $gallery->firstChild);
I want to get information from a form and writing to an xml file by using php ? My xml file is in tree structure
Example of my xml file:
<BookStore>
<Book>
<name>TODO</name>
<url>TODO</url>
</Book>
</BookStore>
when I submit it will create a xml but I want Book and BookStore also as a parent. I created till name and url. But I dont know how to create Book and BookStore too?
You just need to create new parent elements and then append the child.
$bookstore = $doc->createElement('Bookstore');
$book = $doc->createElement("Book");
/* your code */
/* append name and url to $book */
$book->appendChild($textareaNode);
$book->appendChild($textareaNode1);
$bookstore->appendChild($book);
$doc->appendChild($bookstore);
$doc->save("d.xml");
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
All of the examples I can find online about this involve simply adding content to an XML file at the document root, but I really need to do it deeper than that.
My XML file is simple, I have:
<?xml v1 etc>
<channel>
<screenshots>
<item>
<title>Image Title</title>
<link>www.link.com/image.jpg</link>
</item>
</screenshots>
</channel>
All I want to be able to do is add new "item" elements, each with a title and link. I know I need to be using PHP DOM, but I'm stumped as to how to code it so that it adds data within "screenshots" rather than overwriting the whole document. I have a suspicion I may need to use XPath too, but I have no idea how!
The code I have pieced together from online examples looks like this (but I'm certain it's wrong)
$newshottitle = "My new screenshot";
$newshotlink = "http://www.image.com/image.jpg";
$dom = newDomDocument;
$dom->formatOutput = true;
$dom->load("../xml/screenshots.xml");
$dom->getElementsByTagName("screenshots");
$t = $dom->createElement("item");
$t = $dom->createElement("title");
$t->appendChild($dom->createTextNode("$newshottitle"));
$l = $dom->createElement("link");
$l->appendChild($dom->createTextNode("$newshotlink"));
$dom->save("../xml/screenshots.xml");
adding content to an XML file at the document root, but I really need to do it deeper than that.
You're not adding content anywhere at the moment! You create <title> and <link> element nodes with text in, then you do nothing with them. You should be passing them into ‘appendChild’ on the <item> element node (which also you are currently creating and immediately throwing away by not assigning it to a variable).
Here's a starting-point:
$screenshots= $dom->getElementsByTagName("screenshots")[0];
$title= $dom->createElement("title");
$title->appendChild($dom->createTextNode($newshottitle));
$item= $dom->createElement("item");
$item->appendChild($title);
$screenshots->appendChild($item);