Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by artsec
at 2024-08-07 18:58:20
Point:500 Replies:12 POST_ID:828712USER_ID:11484
Topic:
PHP Scripting Language;;
Hi,
I am reading xml strings and saving them in an Array. The array may have several data sets and I have difficulty to extract the data records from each data set of the array. Here is the array output when I do var_dump:
I am reading xml strings and saving them in an Array. The array may have several data sets and I have difficulty to extract the data records from each data set of the array. Here is the array output when I do var_dump:
array(2) { [0]=> object(SimpleXMLElement)#1 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "alexHost.root.net" ["hostname"]=> string(12) "alexHost" ["ip"]=> string(14) "10.10.10.20" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "alex " } } [1]=> object(SimpleXMLElement)#2 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "merryHost.root.net" ["hostname"]=> string(12) "merryHost" ["ip"]=> string(14) "10.10.10.11" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "merry " } } } User 1:
I am looking to have output like this:
adDomain ap
datasource PRODUCT0
dns alexHost.root.net
hostname alexHost
ip 10.10.10.20
os windows 7
userid alex
adDomain ap
datasource PRODUCT0
dns merryHost.root.ne
hostname merryHost
ip 10.10.10.11
os windows 7
userid merry
I tried this with foreach but foreach is not able to process all data sets in the array. It just process the first one. I tried to play with the array pointer but it print either [0] or [1] and when I try to extract data from all data sets of the array it does not do it.
Your help is highly appreciated.
John
Expert: Ray Paseur replied at 2024-08-08 07:01:06
Thanks for the points.
All of the sample scripts on my site named like RAY_temp_* are untrusted, untested, volatile, subject to change or deletion at any time without notice. When I have a test that seems to work, I post the script here at EE. It's safe to copy the EE version of the script, store it on your server, and tailor it for your exact needs.
Cheers, ~Ray
All of the sample scripts on my site named like RAY_temp_* are untrusted, untested, volatile, subject to change or deletion at any time without notice. When I have a test that seems to work, I post the script here at EE. It's safe to copy the EE version of the script, store it on your server, and tailor it for your exact needs.
Cheers, ~Ray
Author: artsec replied at 2024-08-08 06:37:19
Great!
Thanks!
Thanks!
Accepted Solution
Expert: Ray Paseur replied at 2024-08-08 06:15:04
500 points EXCELLENT
This seems to work correctly, too. Moving parts start on line 65. Arrays and objects are very similar when it comes to using foreach().
<?php // RAY_temp_artsec.phperror_reporting(E_ALL);echo '<pre>';// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28207137.html#a39392706// SIMULATED TEST DATA FROM DIFFERENT INPUTS (SEE datasource TAG)$xml1 = <<<EOD<desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT0</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops>EOD;$xml2 = <<<EOD<desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT1</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops>EOD;$xml3 = <<<EOD<desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT2</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops>EOD;// USING SEPARATE OBJECTS$obj1 = SimpleXML_Load_String($xml1);$obj2 = SimpleXML_Load_String($xml2);$obj3 = SimpleXML_Load_String($xml3);// PUT THE OBJECTS INTO AN ARRAY$arr[] = $obj1;$arr[] = $obj2;$arr[] = $obj3;// ACTIVATE THIS TO SEE THE ARRAY// var_dump($arr);// USE ITERATORS TO FIND DATA (PROPERTIES) INSIDE THE OBJECTforeach ($arr as $element){ // THIS IS AN ARRAY OF DESKTOP OBJECTS foreach ($element->desktop as $desktop) { // THIS FINDS THE PROPERTIES OF EACH DESKTOP OBJECT foreach ($desktop as $key => $value) { // FIND AND DISPLAY ONE PROPERTY if ($key == 'datasource') { echo PHP_EOL . "$key => $value"; } } }} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:
Best regards, ~Ray
Author: artsec replied at 2024-08-08 06:13:35
The sample script on your site is giving error.....
Expert: Ray Paseur replied at 2024-08-08 06:08:33
Try this design: http://www.laprbass.com/RAY_temp_artsec.php
The moving parts start at line 55. My thinking here is to preserve the string data from each of the XML documents and use string concatenation to create an array of objects containing all the data. It might also work to convert the strings to objects and concatenate them, which is what i think your code is doing now. I did not test that separately yet.
The moving parts start at line 55. My thinking here is to preserve the string data from each of the XML documents and use string concatenation to create an array of objects containing all the data. It might also work to convert the strings to objects and concatenate them, which is what i think your code is doing now. I did not test that separately yet.
<?php // RAY_temp_artsec.phperror_reporting(E_ALL);echo '<pre>';// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28207137.html#a39392706// SIMULATED TEST DATA FROM DIFFERENT INPUTS (SEE datasource TAG)$xml = <<<EOD<desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT0</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops><desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT1</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops><desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT2</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops>EOD;// WRAP THE XML DOCUMENT$xml = '<thing>' . $xml . '</thing>';// MAKE AN OBJECT|$obj = SimpleXML_Load_String($xml);// ACTIVATE THIS TO SEE THE OBJECT// var_dump($obj);// USE ITERATORS TO FIND DATA (PROPERTIES) INSIDE THE OBJECTforeach ($obj->desktops as $element){ // THIS IS AN ARRAY OF DESKTOP OBJECTS foreach ($element->desktop as $desktop) { // THIS FINDS THE PROPERTIES OF EACH DESKTOP OBJECT foreach ($desktop as $key => $value) { // FIND AND DISPLAY ONE PROPERTY if ($key == 'datasource') { echo PHP_EOL . "$key => $value"; } } }} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:
Author: artsec replied at 2024-08-08 05:55:03
I store the data in an array $xml[] as you can see in my code at line #37. If I do var_dump for $xml then the output is:
array(2) { [0]=> object(SimpleXMLElement)#1 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "alexHost.root.net" ["hostname"]=> string(12) "alexHost" ["ip"]=> string(14) "10.10.10.20" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "alex " } } [1]=> object(SimpleXMLElement)#2 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "merryHost.root.net" ["hostname"]=> string(12) "merryHost" ["ip"]=> string(14) "10.10.10.11" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "merry " } } } User
array(2) { [0]=> object(SimpleXMLElement)#1 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "alexHost.root.net" ["hostname"]=> string(12) "alexHost" ["ip"]=> string(14) "10.10.10.20" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "alex " } } [1]=> object(SimpleXMLElement)#2 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "merryHost.root.net" ["hostname"]=> string(12) "merryHost" ["ip"]=> string(14) "10.10.10.11" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "merry " } } } User
Expert: Ray Paseur replied at 2024-08-08 05:51:15
When you make multiple calls to the API, do you store the data in a file or do you concatenate the data strings together like this?
<desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT0</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops><desktops> <desktop> <adDomain>ap</adDomain> <datasource>PRODUCT0</datasource> <dnsDomain>alexHost.root.net</dnsDomain> <hostname>alexHost</hostname> <ip>10.10.10.20</ip> <os>windows 7</os> <userid>alex</userid> </desktop></desktops> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:
You can probably concatenate the responses and wrap the entire string (multiple XML documents) in a single XML declaration. Then you need one foreach() iterator for each level of data nesting.
Author: artsec replied at 2024-08-08 05:44:01
All,
I have a script which make a query based on IP to an API which is internal and it is not on the Internet. I supply the user names which could be different in quantity each time.
Here is my code:
I have a script which make a query based on IP to an API which is internal and it is not on the Internet. I supply the user names which could be different in quantity each time.
Here is my code:
<HTML><HEAD><TITLE>Desktop Info</TITLE></HEAD><BODY><form action="desktop.php" method=post><p> Please enter P Address :<br><textarea rows="10" cols="100" name="IP-host"></textarea><br><select name="action"><option>None</option><option>Trace Route</option></select><input name="submit" type=submit value="Look Up"></FORM><?phpif(isset($_REQUEST['submit'])){$host_ip = $_REQUEST['IP-host'];$action = $_POST['action'];if (empty($host_ip)){echo "Error: You did not supply IP Address.";exit();}$textarea = explode(PHP_EOL, $host_ip);$base_url = "https://api.net/ttt/";foreach($textarea as $ids){ $url[] = "$base_url$ids";}foreach($url as $final){$file = @file_get_contents($final);$xml[] = simplexml_load_string($file); //Pars XML output.}if (empty($xml)){echo "Error: ","$host_ip", " is not exist in XYZ referential database as a Desktop.";exit();}//var_dump($xml);foreach ($xml as $key => $out){echo "User ID: " .@$out->userid[0]."<br>";echo "Host Name: " .@$out->hostname[0]."<br>";echo "IP Address: " .@$out->ip[0]."<br>";echo "AD Domain: " .@$out->adDomain[0]."<br>";echo "DNS Domain: " .@$out->dnsDomain[0]."<br>";echo "OS: " .@$out->os[0]."<br>";}if ($action == "Trace Route"){$output = shell_exec("tracert $ip2ping");echo "<pre>$output</pre>";}}?></BODY></HTML> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:
The API output is xml with following structure:
<desktops>
<desktop>
<adDomain>ap</adDomain>
<datasource>PRODUCT0</datasource>
<dnsDomain>alexHost.root.net</dnsDomain>
<hostname>alexHost</hostname>
<ip>10.10.10.20</ip>
<os>windows 7</os>
<userid>alex</userid>
</desktop>
</desktops>
Having said that, if I run my script for two IP addresses 10.10.10.20 and 10.10.10.11. My script goes to api and get data and save it into an array. The data in array looks like:
array(2) { [0]=> object(SimpleXMLElement)#1 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "alexHost.root.net" ["hostname"]=> string(12) "alexHost" ["ip"]=> string(14) "10.10.10.20" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "alex " } } [1]=> object(SimpleXMLElement)#2 (1) { ["desktop"]=> object(SimpleXMLElement)#3 (7) { ["adDomain"]=> string(3) "ap" ["datasource"]=> string(3) "PRODUCT0" ["dns"]=> string(27) "merryHost.root.net" ["hostname"]=> string(12) "merryHost" ["ip"]=> string(14) "10.10.10.11" ["os"]=> string(13) "windows 7" ["userid"]=> string(255) "merry " } } } User
I tried this with foreach but foreach is not able to process all data sets in the array. It just process the first one. I tried to play with the array pointer but it print either [0] or [1] and when I try to extract data from all data sets of the array it does not do it.
Thanks,
John
(Edit:Redacted - Modulus_Twelve)
Expert: Ray Paseur replied at 2024-08-08 05:09:15
I think you can use the above array value to help me
Two of the experts have already asked you to post the XML strings. I'll add my voice to that request. The XML is the test data that we need. When you post that test data, we will be able to write the programming that dissects the objects created from the XML. Author: artsec replied at 2024-08-08 03:00:24
Hi,
I think you can use the above array value to help me. I need a way to extract data from above array or arrays with similar structure.
Thanks,
John
I think you can use the above array value to help me. I need a way to extract data from above array or arrays with similar structure.
Thanks,
John
Expert: MunterMan replied at 2024-08-08 02:04:04
Without seeing either the original xml or the code you are using to parse it we can't really help you. However this article may contain some pointers to help you out.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11342-Reading-XML-Namespaces-using-PHP-Without-regex.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11342-Reading-XML-Namespaces-using-PHP-Without-regex.html
Expert: duncanb7 replied at 2024-08-07 20:30:41
Could you send us exact code of the array ? It reports syntax error at my side