im new to XML Handling in PHP
i wrote this script to get variables from post and insert them into a tag with the name of the variable it self from the posted variables and the data the actual data inside this text fields
i have set $id_picture to foo instead of the posted data but with same result
$xml = new DOMDocument('1.0', 'UTF-8');
$id_picture = $_POST['id_picture'];
$xml_id_picture = $xml->createElement("id_picture");
$xml_id_picture_node = $xml->createTextNode($id_picture);
$xml_id_picture->appendChild($xml_id_picture_node);
//upload xml
$xml->save('xml.xml');
what im trying to achieve is save the data from the post to the first variable then i get lost on making it a xml tag and inserting the data in between
<id_picture>foo</id_picture>
You never inserted your new node into the main object. You need something like
$xml->appendChild($xml_id_picture);
so that your newly created id_picture node will actually show up in your document.
You have to add the node to the document.
$xml = new DOMDocument('1.0', 'UTF-8');
$id_picture = $_POST['id_picture'];
$xml_id_picture = $xml->createElement("id_picture");
$xml_id_picture_node = $xml->createTextNode($id_picture);
$xml_id_picture->appendChild($xml_id_picture_node);
$xml->appendChild($xml_id_picture);//<-- here
//upload xml
$xml->save('xml.xml');
http://codepad.org/6Ml2cUYe
It looks like you are creating an element, appending a node to it, but then you are not appending the element to the document.
Related
I am pulling HTML from Selenium, and then extracting data from the HTML using Xpaths.
This is the Xpath:
/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a
This is my code:
$data = $webdriver->getPageSource();
d($data, $urltemplate);
$doc = new DOMDocument();
$doc->loadHTML($data);
$xp = "/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a";
$xpatho = new DOMXpath($doc);
$elementsn = $xpatho->query($xp);
d(get_class($elementsn),$elementsn->count(),$xp,$name);
// d() is a custom function like var_dump().
I always get $elementsn->count() = 0.
This is $data:
https://pastebin.com/ahuvkJfN
I am trying to extract those strings like "NAD M10 BLUOS...", "NAD M12 DIRECT DIGITAL..." and so on...
I saved the HTML into a file, and opened it in my browser. I am attaching screenshot of what data I was looking to retrieve (highlighted in blue):
Basically, the HTML page is a product listing, and I am looking to extract all the product names. To confirm, I used Chrome Developer tools, and used the copy full Xpath function. I have the following Xpaths for some of the product names:
/html/body/div[2]/div[1]/div/div/div/div/ul/li[1]/div[1]/h3/a
/html/body/div[2]/div[1]/div/div/div/div/ul/li[3]/div[1]/h3/a
I would guess that this would generalise to:
/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a
However, I keep on getting a DOMNodeList with count = 0. Why is this so, and how can I check what the error is, if any?
P.S.: This is the original webpage: http://lenbrook.com.sg/3-shop-by-brand#/page-4/price-49-8667
Try changing your $xp
$xp = '//a[#class="product_link"]/text()'
I have a form which on POST creates an XML based on the values in the form. I am using DOM DOCUMENT to create XML. I am giving exact element names when creating xml but when it is created the element names are in lowercase which my API is not accepting. for eg.
I am giving this as an input <LSP_Name>JaVAS</LSP_Name> and when it is created it forms like this <lsp_name>JaVAS</lsp_name>
I tried $xml->formatOutput=true; but had no luck.
Any one who had faced this similar issue ?
DOCUMENT CREATION
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput=true;
$root = $xml->createElement("NewDataSet");
$xml->appendChild($root);
$LSP_Code = $xml->createElement("LSP_Code");
$LSP_CodeText = $xml->createTextNode(LSP_CODE);
$LSP_Code->appendChild($LSP_CodeText);
$docket = $xml->createElement("Docket");
$root->appendChild($docket);
$docket->appendChild($LSP_Code);
TIA
I have installed a JSON plugin and got the content of HTML page. Now I want to parse and find a particular table, which has only class, but no id. I parse it using the PHP class DOMDocument.I have the idea to access the tag before the table and after that somehow to access the next following tag(my table) using DOMDocument.
Example:
<a name="Telefonliste" id="Telefonliste"></a>
<table class="wikitable">
So, i get fist the <a> and after that I get <table>.
I have got all the tables using the following commands and especially getElementsByTagName(). After that I can access item(2) where my table is:
$dom = new DOMDocument();
//load html source
$html = $dom->loadHTML($myHtml);
//discard white space
$dom->preserveWhiteSpace = false;
//the table by its tag name
$table = $dom->getElementsByTagName('table');
$rows = $table->item(2)->getElementsByTagName('tr');
This way is ok, but I want to make it more general, because now I know that the table is located in item(2), but the location can be changed e.g if a new table is included in the HTML page before my table. My table will not be in item(2), but in item(3). So, I want it it to parse in a way that I can still reach this table without changing something in my code. Can I do it using DOMDocument as a DOM parser?
You can use DOMXPath, and make the expression as general as you need it.
For example:
$dom = new DOMDocument();
//discard white space
$dom->preserveWhiteSpace = false;
//load html source
$dom->loadHTML($myHtml);
$domxpath = new DOMXPath($dom);
$table = $domxpath->query('//table[#class="wikitable" and not(#id)][0]')->item(0);
$elementBeforeTable = $table->previousSibling;
$rows = $table->getElementsByTagName('tr');
I've started writing a simple extension of this for the purpose of web scraping. I'm not 100% on the direction I want to take with it yet, but you can see an example of how to get the original HTML back in the response of the search rather than just raw text.
https://github.com/WolfeDev/PageScraper
EDIT: I plan on implementing basic table parsing soon.
The situation is as follows. I have to send a defined XML to an url http:\\www.example.com:1234 with some variables that I have to previously define.
XML is like this:
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
But, I want to define those variables (1, 2) within an html/php form and get method, so the user can introduce both variables and then click on the submit button from the form to send the XML to the previously URL.
Also, XML should have the "Content-Type","application/x-www-form-urlencoded" header.
Is this possible? I've tried to pass these variables directly to the XML and the best that I've come to is to showing the XML and not parsing the php strings.
Also, I've tried some scripts like simplexml from PHP classes, but with no luck so far.
1) To modify existing XML with new values . Try this
sample.xml :
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
PHP :
$xml = simplexml_load_file("sample.xml");
$xml->Title3 = $_GET['t3']; // Updating <Title3></Title3> from GET method
$xml->Title5[0]->Title51 = $_GET['t5']; // Updating <Title51></Title51> from GET method
$xml->asXML('sample.xml'); // saving the xml file
2)To create new XML file (sample.xml) :
PHP:
$xml = new SimpleXMLElement("<Title1></Title1>");
$xml->Title2='Some Text';
$xml->Title3 = $_GET['t3'];
$xml->Title4='Some Text';
$xml->Title5[0]->Title51 = $_GET['t5'];
$xml->asXML('sample.xml'); // saving the xml file
I have showed you both possibilities mentioned in the comment . Use anyone which comforts you :)
I'm making an interface-website to update a concert-list on a band-website.
The list is stored as an XML file an has this structure :
I already wrote a script that enables me to add a new gig to the list, this was relatively easy...
Now I want to write a script that enables me to edit a certain gig in the list.
Every Gig is Unique because of the first attribute : "id" .
I want to use this reference to edit the other attributes in that Node.
My PHP is very poor, so I hope someone could put me on the good foot here...
My PHP script :
Well i dunno what your XML structure looks like but:
<gig id="someid">
<venue></venue>
<day></day>
<month></month>
<year></year>
</gig>
$xml = new SimpleXmlElement('gig.xml',null, true);
$gig = $xml->xpath('//gig[#id="'.$_POST['id'].'"]');
$gig->venue = $_POST['venue'];
$gig->month = $_POST['month'];
// etc..
$xml->asXml('gig.xml)'; // save back to file
now if instead all these data points are attributes you can use $gig->attributes()->venue to access it.
There is no need for the loop really unless you are doing multiple updates with one post - you can get at any specific record via an XPAth query. SimpleXML is also a lot lighter and a lot easier to use for this type of thing than DOMDOcument - especially as you arent using the feature of DOMDocument.
You'll want to load the xml file in a domdocument with
<?
$xml = new DOMDocument();
$xml->load("xmlfile.xml");
//find the tags that you want to update
$tags = $xml->getElementsByTagName("GIG");
//find the tag with the id you want to update
foreach ($tags as $tag) {
if($tag->getAttribute("id") == $id) { //found the tag, now update the attribute
$tag->setAttribute("[attributeName]", "[attributeValue]");
}
}
//save the xml
$xml->save();
?>
code is untested, but it's a general idea