PHP XML DOM getElementById - php

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);

Related

insertBefore xml php

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);

How to extract in php this type of XML format?

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.

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');

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

How to get a hyperlink in an XML file?

I want to set hyperlink in the image in XML file.
Here is my code of XML file:
<logos>
<logo id="1" name="Abc" path="abc.jpg" x="23" y="4" height="10" width="60"/>
<logo id="2" name="Xya" path="xyz.jpg" x="50" y="`4" height="20" width="40"/>
</logos>
I want to set hyperlink in this image.
XML is a generic data format. It doesn't have any hyperlink capabilities. A specific XML application can (XHTML, for example, has the a element).
If the XML application you are using doesn't include anything to describe hyperlinks, then you need to change it, possibly by importing something from another namespace (such as XLink).
The software that consumes the application will almost certainly have to be updated to add support for the change you make to the language.
I would set an attribute in the node for link_out like:
<logo link_out=""...
Or create a child element to the Logo node if multiple links will ever be in use.
Really hard to understand your need for this.
what XML structure are you following? If you aren't following one, what is stopping you from doing something like:
<logos>
<logo>
<image blah="">
<link blah="">
</logo>
</logos>
Hard to help you when there are a myriad of solutions, but we don't have all the details.
In the XML file, you need to tell it to ignore tags that are intended as html
tags and not tags of the xml structure by enclosing the html using
<![CDATA[ your html ]]>
<urlLink><![CDATA[ Click Here ]]></urlLink>

Categories