add or update node value with array php - php

Sorry for possible newbie question, but I need to use xml for an import. I've figured it out so far but then scratched my eyeballs out while trying to figure out this (and searched for it)
I have this
<?php
$xml = new DOMDocument("1.0","UTF-8");
$container = $xml-> createElement("container");
$container = $xml-> appendChild($container);
for($n=0;$n<8;$n++){
$maxnum[]=$n;
}
$xtags = $xml->CreateElement("tags",$maxnum); $xtags = $container->appendChild($xtags);
if($xml->save("stuff2.xml")){echo 'success';} else {echo 'not working';}
?>
Basically I dont want to create a child node for ever value of a loop. And I know thats not the proper way to do it, nor is array_values and so on. But it's what I want to achieve. I need to generate an XML file for wpall import so that I can succesfully bulk import to woocommerce. The specific attribute here i ize, and Im getting that usingfile_get_contents` from the server, parsing which xpath with a foreach loop. Problem is I want them all in a single field rather than having this
<size>
<value>1</value>
<value>2</value>
</size>
and so on, when I want it like this <size><value>1,2,3....</value></size>
Any help, please?

Then you do not need to use an array,
you can do something like:
echo "<size><value>";
foreach($n as $n){
$string="$string, ".$n;
}
echo $string;
echo" </value></size>";

Related

How to get all data from multi dimesnion object array when it changes all the time

At start i made html table for specific xml array,
it worked.But then i tested my code on others and it failed read all levels :(
here are 2 arrays that try to access manually
$newaCref->Debts->LiabilityDebts[$i]->Debt->Sum[$b]->Total
$newaCref->Debts->LiabilityDebts[$k]->Debt[$j]->Sum->Total
to access them first parts $newaCref->Debts->LiabilityDebts are the same but then it changes and goes even deeper.
My question is how to make it go automatically through all levels i need?
This is how i do it now for each <td> row and $newaCref is result of XML
$newaCref = new SimpleXMLElement($xmldataC, LIBXML_NOCDATA);
for($i=0;$i<count($newaCref->Debts->LiabilityDebts);$i++){
for($b=0;$b<count($newaCref->Debts->LiabilityDebts[$i]->Debt->Sum);$b++){
foreach($newaCref->Liabilities->Liability[$i]->Sum[$b]->Total as $row){
echo '<td>'.$row.'</td>';
}
}
}
Not testet, just to explain:
$newaCref = new SimpleXMLElement($xmldataC, LIBXML_NOCDATA);
$result = $xml->xpath('/Debts/LiabilityDebts');
while(list( , $node) = each($result)) {
$subresult = $node->xpath('Debt/Sum');
while(list( , $subnode) = each($subresult)) {
echo '<td>'.((string)$subnode).'</td>';
}
}
Notes:
In your example the inner foreach makes no sence at this point.
$newaCref->Liabilities->Liability[$i]
The xpath for this would be /Liabilities/Liability
If you are showing $xmldataC in your example, why are you saying #RiggsFolly i cant do it :( and not just do print_r(htmlentities($xmldataC))?
So take this as example and get it work for your needs. But keep in mind, xml can be tricky if you dont now how they work, especially in PHP.
:)

Pulling SimpleXML data Across a Page

I'm sure there's a pretty obvious solution to this problem...but it's alluding me.
I've got an XML feed that I want to pull information from - from only items with a specific ID. Let lets say we have the following XML:
<XML>
<item>
<name>John</name>
<p:id>1</id>
<p:eye>Blue</eye>
<p:hair>Black</hair>
</item>
<item>
<name>Jake</name>
<p:id>2</id>
<p:eye>Hazel</eye>
<p:hair>White</hair>
</item>
<item>
<name>Amy</name>
<p:id>3</id>
<p:eye>Brown</eye>
<p:hair>Yellow</hair>
</item>
<item>
<name>Tammy</name>
<p:id>4</id>
<p:eye>Blue</eye>
<p:hair>Black</hair>
</item>
<item>
<name>Blake</name>
<p:id>5</id>
<p:eye>Green</eye>
<p:hair>Red</hair>
</item>
</xml>
And I want to pull ONLY people with the ID 3 and 1 into specific spots on a page (there will be no double IDs - unique IDs for each item). Using SimpleXML and a forloop I can easily display each ITEM on a page using PHP - with some "if ($item->{'id'} == #)" statements (where # is the ID I'm looking for(, I can also display the info for each ID I'm looking for.
The problem I'm running into is how to distribute the information across the page. I'm trying to pull the information into specific spots on a page my first attempt at distributing the specific fields across the page aren't working as follows:
<html>
<head><title>.</title></head>
<body>
<?php
(SimpleXML code / For Loop for each element here...)
?>
<H1>Staff Profiles</h1>
<h4>Maintenance</h4>
<p>Maintenance staff does a lot of work! Meet your super maintenance staff:</p>
<?php
if($ID == 1) {
echo "Name:".$name."<br/>";
echo "Eye Color:".$eye."<br/>";
echo "Hair Color:".$hair."<br/>";
?>
<h4>Receptionists</h4>
<p>Always a smiling face - meet them here:</p>
<?php
if($ID == 3) {
echo "Name:".$name."<br/>";
echo "Eye Color:".$eye."<br/>";
echo "Hair Color:".$hair."<br/>";
?>
<H4>The ENd</h4>
<?php (closing the four loop) ?>
</body>
</html>
But it's not working - it randomly starts repeating elements on my page (not even the XML elements). My method is probably pretty...rudimentary; so a point in the right direction is much appreciated. Any advice?
EDIT:
New (NEW) XPATH code:
$count = 0;
foreach ($sxe->xpath('//item') as $item) {
$item->registerXPathNamespace('p', 'http://www.example.com/this');
$id = $item->xpath('//p:id');
echo $id[$count] . "\n";
echo $item->name . "<br />";
$count++;
}
use xpath to accomplish this, and write a small function to retrieve a person by id.
function getPerson($id = 0, &$xml) {
return $xml->xpath("//item[id='$id']")[0]; // PHP >= 5.4 required
}
$xml = simplexml_load_string($x); // assume XML in $x
Now, you can (example 1):
echo getPerson(5, $xml)->name;
Output:
Blake
or (example 2):
$a = getPerson(2, $xml);
echo "$a->name has $a->eye eyes and $a->hair hair.";
Output:
Jake has Hazel eyes and White hair.
see it working: http://codepad.viper-7.com/SwLids
EDIT In your HTML, this would probably look like this:
...
<h1>Staff Profiles</h1>
<h4>Maintenance</h4>
<p>Maintenance staff does a lot of work! Meet your super maintenance staff:</p>
<?php
$p = getPerson(4, $xml);
echo "Name: $p->name <br />";
echo "Eye Color: $p->eye <br />";
echo "Hair Color: $p->hair <br />";
?>
no looping required, though.
First thing that popped into my mind is to use a numerical offset (which is zero-based in SimpleXML) as there is a string co-relation between the offset and the ID, the offset is always the ID minus one:
$items = $xml->item;
$id = 3;
$person = $items[$id - 1];
echo $person->id, "\n"; // prints "3"
But that would work only if - and only if - the first element would have ID 1 and then each next element the ID value one higher than it's previous sibling.
Which we could just assume by the sample XML given, however, I somewhat guess this is not the case. So the next thing that can be done is to still use the offset but this time create a map between IDs and offsets:
$items = $xml->item;
$offset = 0;
$idMap = [];
foreach ($items as $item) {
$idMap[$item->id] = $offset;
$offset++;
}
With that new $idMap map, you then can get each item based on the ID:
$id = 3;
$person = $items[$idMap[$id]];
Such a map is useful in case you know that you need that more than once, because creating the map is somewhat extra work you need to do.
So let's see if there ain't something built-in that solves the issue already. Maybe there is some code out there that shows how to find an element in simplexml with a specific attribute value?
SimpleXML: Selecting Elements Which Have A Certain Attribute Value (Reference Question)
Read and take value of XML attributes - Especially because of the answer on how to add the functionality to SimpleXMLElement transparently.
Which leads to the point you could do it as outlined in that answer that shows how it works transparently like this:
$person = $items->attribute("id", $id);
I hope this is helpful.

How to import XML file into MariaDB database?

I have a problem. My PHP script not working and I dont know why. This is XML file:
http://itoffice.sk/toto.xml
And this is my PHP code for read XML:
$xml = simplexml_load_file("toto.xml") or die("feed sa nepodarilo načítať");
$id = $xml->Zbozi['kod_zbozi'];
echo $id;
This not working. I need show and write into MySQL all attributes of <Zbozi>
<Zbozi> example:
<Zbozi diffgr:id="Zbozi1" msdata:rowOrder="0" kod_zbozi="DL139A" popis="HP DMS-59 to Dual DVI Cable Kit,accessory" part_number="DL139A" zaruka="24" dostupnost="false" dph="20" cena="29.23" cenaEU="35.34" snimek="DL139A/06_s.jpg" kod_vyrobce="093-001453" kod_kategorie="PC STOLNÍ POČÍTAČE" kategorie="Stolné počítače" kod_podkategorie="PŘÍSLUŠENSTVÍ ZN PC" podkategorie="Príslušenstvo značkových PC" typ="0" aut_poplatek="0.000" rema="0.000" sam_neprodejne="0" sn_vydej="false" ean="808736662901" doprodej="false">
Help me please. Thanks.
The reason your script was not working, is because you weren't following the XML tree properly. As you can see, your XML's structure looks like this:
<diffgr:diffgram>
<Cenik>
<Zbozi></Zbozi>
...
<Zbozi></Zbozi>
</Cenik>
</diffgr:diffgram>
Therefore you would have to follow that tree, and then loop for the Zbozi elements in order to get all of their attributes.
Take a look at this code:
$xml = simplexml_load_file('http://itoffice.sk/toto.xml');
$zbozi = $xml->children('diffgr', TRUE)->diffgram->children()->Cenik->Zbozi;
$id_arr = array();
foreach ($zbozi as $zbozi_element)
{
$id_arr[] = (string) $zbozi_element['kod_zbozi'];
}
First we load the XML file, nothing new there. But in the second line, we are traversing down the XML file. First we set the prefix namespace to diffgr (because of the semicolon in diffgr:diffgram), then we simply find the Zbozi elements, and loop for them.
You can see that in the loop I've added all of the kod_zbozi attribute values into an array. You can really do anything you'd like with that information at this point, this was just to get you started.

Use PHP to load XML Data into Oracle

I'm fairly new to php although I've been programming for a couple years.
I'm working on a project and the end goal is to load certain elements of an xml file into an oracle table on a nightly basis. I have a script which runs nightly and saves a the file on my local machine. I've searched endlessly for answers but have been unsuccessful.
Here is an aggregated example of the xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<Report account="7869" start_time="2012-02-23T00:00:00+00:00" end_time="2012-02-23T15:27:59+00:00" user="twilson" more_sessions="false">
<Session id="ID742247692" realTimeID="4306650378">
<Visitor id="5390643113837">
<ip>128.XXX.XX.XX</ip>
<agent>MSIE 8.0</agent>
</Visitor>
</Session>
<Session id="ID742247695" realTimeID="4306650379">
<Visitor id="7110455516320">
<ip>173.XX.XX.XXX</ip>
<agent>Chrome 17.0.963.56</agent>
</Visitor>
</Session>
</Report>
One thing to note is that the xml file will contain several objects which I will need to load into my table and the above example would just be for two rows of data. I'm familiar with the whole process of connecting and loading data into oracle and have setup similar scripts which perform ETL of txt. and csv. files using php. Unfortunately for me in this case the data is stored in xml. The approach I've taken when loading a csv. file is to load the data into an array and proceed from there.
I'm pretty certain that I can use something similar and perhaps create variable for each or something similar but am not really too sure how to do that with an xml. file.
$xml = simplexml_load_file('C:/Dev/report.xml');
echo $xml->Report->Session->Visitor->agent;
In the above code i'm trying to just return the agent associated with each visitor. This returns an error 'Trying to get property of non-object in C:\PHP\chatTest.php on line 11'
The end result would be for me to load the data into a table similar to the example I provided would be to load two rows into my table which would look similar to below however I think I can handle that if i'm able to get the data into an array or something similar.
IP|AGENT
128.XXX.XX.XX MSIE 8.0
173.XX.XX.XXX Chrome 17.0.963.56
Any help would be greatly appreciated.
Revised Code:
$doc = new DOMDocument();
$doc->load( 'C:/Dev/report.xml' );
$sessions = $doc->getElementsByTagName( "Session" );
foreach( $sessions as $session )
{
$visitors = $session->getElementsByTagName( "Visitor" );
foreach( $visitors as $visitor )
$sessionid = $session->getAttribute( 'realTimeID' );
{
$ips = $visitor->getElementsByTagName( "ip" );
$ip = $ips->item(0)->nodeValue;
$agents = $visitor->getElementsByTagName( "agent" );
$agent = $ips->item(0)->nodeValue;
echo "$sessionid- $ip- $agent\n";
}}
?>
The -> operator in PHP means that you are trying to invoke a field or method on an object. Since Report is not a method within $xml, you are receiving the error that you are trying to invoke a property on a non-object.
You can try something like this (don't know if it works, didn't test it and haven't written PHP for a long time, but you can google it):
$doc = new DOMDocument();
$doc->loadXML($content);
foreach ($doc->getElementsByTagName('Session') as $node)
{
$agent = $node->getElementsByTagName('Visitor')->item(0)->getElementsByTagName('agent')->item(0)->nodeValue;
}
edit:
Adding stuff to an array in PHP is easy as this:
$arr = array();
$arr[] = "some data";
$arr[] = "some more data";
The PHP arrays should be seen as a list, since they can be resized on the fly.
I was able to figure this out using simplexml_load_file rather than the DOM approach. Although DOM works after modifying the Leon's suggestion the approach below is what I would suggest.
$xml_object = simplexml_load_file('C:/Dev/report.xml');
foreach($xml_object->Session as $session) {
foreach($session->Visitor as $visitor) {
$ip = $visitor->ip;
$agent = $visitor->agent;
}
echo $ip.','.$agent."\n";
}

Not finding elements using getElementsByTagName() using DOMDocument

I'm trying to loop through multiple <LineItemInfo> products contained within a <LineItems> within XML I'm parsing to pull product Ids out and send emails and do other actions for each product.
The problem is that it's not returning anything. I've verified that the XML data is valid and it does contain the necessary components.
$itemListObject = $orderXML->getElementsByTagName('LineItemInfo');
var_dump($itemListObject->length);
var_dump($itemListObject);
The output of the var_dump is:
int(0)
object(DOMNodeList)#22 (0) {
}
This is my first time messing with this and it's taken me a couple of hours but I can't figure it out. Any advice would be awesome.
EDIT:
My XML looks like this... except with a lot more tags than just ProductId
<LineItems>
<LineItemInfo>
<ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
</LineItemInfo>
<LineItemInfo>
<ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
</LineItemInfo>
</LineItems>
Executing the following code does NOT get me the ProductId
$itemListObject = $orderXML->getElementsByTagName('LineItemInfo');
foreach ($itemListObject as $element) {
$product = $element->getElementsByTagName('ProductId');
$productId = $product->item(0)->nodeValue;
echo $productId.'-';
}
EDIT #2
As a side note, calling
$element->item(0)->nodeValue
on $element instead of $product caused my script's execution to discontinue and not throwing any errors that were logged by the server. It's a pain to debug when you have to run a credit card to find out whether it's functioning or not.
DOMDocument stuff can be tricky to get a handle on, because functions such as print_r() and var_dump() don't necessarily perform the same as they would on normal arrays and objects (see this comment in the manual).
You have to use various functions and properties of the document nodes to pull out the data. For instance, if you had the following XML:
<LineItemInfo attr1="hi">This is a line item.</LineItemInfo>
You could output various parts of that using:
$itemListObjects = $orderXML->getElementsByTagName('LineItemInfo');
foreach($itemListObjects as $node) {
echo $node->nodeValue; //echos "This is a line item."
echo $node->attributes->getNamedItem('attr1')->nodeValue; //echos "hi"
}
If you had a nested structure, you can follow basically the same procedure using the childNodes property. For example, if you had this:
<LineItemInfo attr1="hi">
<LineItem>Line 1</LineItem>
<LineItem>Line 2</LineItem>
</LineItemInfo>
You might do something like this:
$itemListObjects = $orderXML->getElementsByTagName('LineItemInfo');
foreach($itemListObjects as $node) {
if ($node->hasChildNodes()) {
foreach($node->childNodes as $c) {
echo $c->nodeValue .",";
}
}
}
//you'll get output of "Line 1,Line 2,"
Hope that helps.
EDIT for specific code and XML
I ran the following code in a test script, and it seemed to work for me. Can you be more specific about what's not working? I used your code exactly, except for the first two lines that create the document. Are you using loadXML() over loadHTML()? Are there any errors?
$orderXML = new DOMDocument();
$orderXML->loadXML("
<LineItems>
<LineItemInfo>
<ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
</LineItemInfo>
<LineItemInfo>
<ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
</LineItemInfo>
</LineItems>
");
$itemListObject = $orderXML->getElementsByTagName('LineItemInfo');
foreach ($itemListObject as $element) {
$product = $element->getElementsByTagName('ProductId');
$productId = $product->item(0)->nodeValue;
echo $productId.'-';
}
//outputs "149593-149595-"
XML tags tend to be lower-camel-case (or just "camel-case"), i.e. "lineItemInfo", instead of "LineItemInfo" and XML is case-sensitive, so check for that.

Categories