simplexml load multiple files at once - php

I'm using XML to fetch some language data.
The XML markup is as follows:
<language>
<item>
<key>KEY</key>
<string>STRING</string>
</item>
<item>
....
</item>
</language>
I have 2 XML-files with the same markup. I want to load them both with simplexml_load_file. Does anyone know what's the best approach to do this?
I tried something like this:
<?php
$xml = simplexml_load_file('...url_file_1...');
$xml .= simplexml_load_file('...url_file_2...');
?>
But thats not working. But thats what I want: all the data from the 2 (and eventually more) files in one variable.
Edit: I want to make use of the simpleXML library if possible.

Related

How to parse xml subtree in php

Hello have worten some php code to parse a xml file as long if i use seperate fields this works like a charm but how i can call a subtree ?
like eg :
<item>
<items>
<date>
<from>2020-03-23</from>
<until>2020-03-27</until>
</date>
</items>
</item>
```
I assume with subtree you mean an element within an element etc.
PHP offers a built in solution for this: simplexml_load_string
$xml = <<<BLOCK
<item>
<items>
<date>
<from>2020-03-23</from>
<until>2020-03-27</until>
</date>
</items>
</item>
BLOCK;
var_dump(simplexml_load_string($xml));
You can read a file directly aswell by using simplexml_load_file.
i have found a solution for this problem i have done the following:
<?php
error_reporting(E_ALL);
$xml_file = simplexml_load_file("converted.xml");
echo $xml_file->date[0]->from;
echo $xml_file->date[0]->until;
?>
```

Make use of an index file for parsing a large XML file

I'm working on a Wikipedia XML Dump file (15GB), which is provided with an index text file.. looking like this :
1628813:431:Bille August
1628813:434:Blues
1628813:435:Bioéthique
1628813:436:Brive-la-Gaillarde
1628813:438:Burdigala
1628813:439:Bouliac
The XML file is basically in this format:
<page>
<title> </title>
<id> </id>
<revision>
<id> </id>
...
<text> </text>
</revision>
</page>
What I want is to get the Text tag content corresponding to specific page Titles.
How can I make use of the index file for quick parsing/searching to achieve this?
P.S: I already tried many solutions for quick parsing and nothing worked for my needs..
I'm working with PHP, I tried SAX parsing, which is quick but didn't allow me to handle the nodes the way I wanted, and tried combining XMLReader and SimpleXML, but it was too slow..

Pass PHP variable as a parameter to XSL

I have a PHP page that outputs XML by changing the header type and outputting the xml with an XSL stylesheet for an RSS feed:
<?php
header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo '<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/path/feed.xsl"?>' . "\n";
?>
<channel>
<item>...</item>...
</channel>
I want to pass some PHP variables from the original page to the XSL, how do I do this?
Example:
I have the variables...
$header = "This is a cool page";
$description = "This is a description";
...that I want to pass to the XSL page and use within it meaning the title can be changed through PHP and dynamically changed in the XSL rather than hard coding it.
It cannot be passed through XML because I am using the XSL as a fallback and therefore the title should not be displayed when the browser supports RSS. Also it must come from that page rather than referencing another file with that variable.
Something like
<?xml-stylesheet title="XSL_formatting" type="text/xsl" param-header="<?=$header?>' param-description="<?=$description?>' href="/path/feed.xsl"?>
Let me know if there is a better way to achieve this.
*UPDATE - This doesn't work because the extra tags don't validate because they aren't part of a namespace.
Actually forgot to put the rss tags into the question but when I added XML to store the PHP variables between the rss and channel tags, I could use them in the XSL file dynamically and the XML used to store them doesn't render when viewed in an RSS reader, e.g.
<rss>
<extraInfo>
<heading>...</heading>
<description>...</description>
</extraInfo>
<channel>
...
</channel>
</rss>

update element from node in XML

First of all, I just found this site a few days ago and am very happy it exists.
I am facing a problem with updating a child element from a XML Node.
The xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
<book>
<id>1</id>
<bookname>Title 1</bookname>
<bookurl>SomeURLToMyBook</bookurl>
<clicks>0</clicks>
</book>
<book>
<id>2</id>
<bookname>Title 2</bookname>
<bookurl>SomeURLToMyBook</bookurl>
<clicks>0</clicks>
</book>
</books>
I do know how to retrieve the node for book with (e.g.) ID 2 using:
$xml= simplexml_load_file('books.xml);
I then use xpath to find the correct node. as in:
$booknum= $_GET('booklist'); //booklist is the parameter in the querystring
$arrOutput = $xml->xpath("//*[id='".$booknum."']");
What I try to achieve is to update the clicks element and then save the outcome into the existing XML.
I found several code examples on this site but neither one seems to work for me.
I probably do something wrong but I am not really familiar with PHP (learning new things every day!)
So if anybody is willing to help me out I would be grateful.
TIA

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

Categories