Creating objects from xml-data
$begingroup$
I've got an xml file with some info about hosts connected to my home network and I would like to create objects containing that info in a neat way. So that I can make some nice front-end application with that data later on.
This is how I do it:
- Loading the xml
- Encoding it into json
- Making an array from the json
- Making a simpler array from the last array
- Creates objects from the array
- Done!
My question to you is: Is this the way to do it, or is there some better way? Am I missing something obvious? Is this the way to do it the "oop" way?
Trying to learn this, so all constructive feedback is welcome!
class Host {
public $ipv4, $mac;
public function __construct($ipv4, $mac){
$this->ipv4 = $ipv4;
$this->mac = $mac;
}
}
class PagesController extends Controller
{
public function index(){
// Get xml with hosts that are connected to local network
$xml = file_get_contents("C:/test.xml");
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// put all hosts in separate array (to make it easier to work with when creating an object further down)
foreach ($array['host'] as $hostKey => $hostValue) {
foreach ($hostValue['address'] as $addressKey => $addressValue) {
foreach ($addressValue['@attributes'] as $attributeKey => $attributeValue) {
$hosts['host'.$hostKey]['address'.$addressKey][$attributeKey] = $attributeValue;
}
}
}
// Create object from array created earlier
foreach ($hosts as $hostKey => $hostValue) {
if(isset($hostValue)){
foreach ($hostValue as $addressKey => $addressValue) {
if(isset($addressValue)){
if(isset($addressValue['addrtype'])){
// ipv4 ?
if($addressValue['addrtype'] == 'ipv4'){
$ipv4 = $addressValue['addr'];
// or mac-address?
} elseif($addressValue['addrtype'] == 'mac'){
$mac = $addressValue['addr'];
}
}
}
}
}
// Skapa ett objekt som heter host+$hostKey
$hosts[$hostKey] = new Host($ipv4, $mac);
}
Note: The reason for the PagesController class and the index() method is cause I'm using the laravel framework.
php object-oriented xml
New contributor
$endgroup$
add a comment |
$begingroup$
I've got an xml file with some info about hosts connected to my home network and I would like to create objects containing that info in a neat way. So that I can make some nice front-end application with that data later on.
This is how I do it:
- Loading the xml
- Encoding it into json
- Making an array from the json
- Making a simpler array from the last array
- Creates objects from the array
- Done!
My question to you is: Is this the way to do it, or is there some better way? Am I missing something obvious? Is this the way to do it the "oop" way?
Trying to learn this, so all constructive feedback is welcome!
class Host {
public $ipv4, $mac;
public function __construct($ipv4, $mac){
$this->ipv4 = $ipv4;
$this->mac = $mac;
}
}
class PagesController extends Controller
{
public function index(){
// Get xml with hosts that are connected to local network
$xml = file_get_contents("C:/test.xml");
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// put all hosts in separate array (to make it easier to work with when creating an object further down)
foreach ($array['host'] as $hostKey => $hostValue) {
foreach ($hostValue['address'] as $addressKey => $addressValue) {
foreach ($addressValue['@attributes'] as $attributeKey => $attributeValue) {
$hosts['host'.$hostKey]['address'.$addressKey][$attributeKey] = $attributeValue;
}
}
}
// Create object from array created earlier
foreach ($hosts as $hostKey => $hostValue) {
if(isset($hostValue)){
foreach ($hostValue as $addressKey => $addressValue) {
if(isset($addressValue)){
if(isset($addressValue['addrtype'])){
// ipv4 ?
if($addressValue['addrtype'] == 'ipv4'){
$ipv4 = $addressValue['addr'];
// or mac-address?
} elseif($addressValue['addrtype'] == 'mac'){
$mac = $addressValue['addr'];
}
}
}
}
}
// Skapa ett objekt som heter host+$hostKey
$hosts[$hostKey] = new Host($ipv4, $mac);
}
Note: The reason for the PagesController class and the index() method is cause I'm using the laravel framework.
php object-oriented xml
New contributor
$endgroup$
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago
add a comment |
$begingroup$
I've got an xml file with some info about hosts connected to my home network and I would like to create objects containing that info in a neat way. So that I can make some nice front-end application with that data later on.
This is how I do it:
- Loading the xml
- Encoding it into json
- Making an array from the json
- Making a simpler array from the last array
- Creates objects from the array
- Done!
My question to you is: Is this the way to do it, or is there some better way? Am I missing something obvious? Is this the way to do it the "oop" way?
Trying to learn this, so all constructive feedback is welcome!
class Host {
public $ipv4, $mac;
public function __construct($ipv4, $mac){
$this->ipv4 = $ipv4;
$this->mac = $mac;
}
}
class PagesController extends Controller
{
public function index(){
// Get xml with hosts that are connected to local network
$xml = file_get_contents("C:/test.xml");
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// put all hosts in separate array (to make it easier to work with when creating an object further down)
foreach ($array['host'] as $hostKey => $hostValue) {
foreach ($hostValue['address'] as $addressKey => $addressValue) {
foreach ($addressValue['@attributes'] as $attributeKey => $attributeValue) {
$hosts['host'.$hostKey]['address'.$addressKey][$attributeKey] = $attributeValue;
}
}
}
// Create object from array created earlier
foreach ($hosts as $hostKey => $hostValue) {
if(isset($hostValue)){
foreach ($hostValue as $addressKey => $addressValue) {
if(isset($addressValue)){
if(isset($addressValue['addrtype'])){
// ipv4 ?
if($addressValue['addrtype'] == 'ipv4'){
$ipv4 = $addressValue['addr'];
// or mac-address?
} elseif($addressValue['addrtype'] == 'mac'){
$mac = $addressValue['addr'];
}
}
}
}
}
// Skapa ett objekt som heter host+$hostKey
$hosts[$hostKey] = new Host($ipv4, $mac);
}
Note: The reason for the PagesController class and the index() method is cause I'm using the laravel framework.
php object-oriented xml
New contributor
$endgroup$
I've got an xml file with some info about hosts connected to my home network and I would like to create objects containing that info in a neat way. So that I can make some nice front-end application with that data later on.
This is how I do it:
- Loading the xml
- Encoding it into json
- Making an array from the json
- Making a simpler array from the last array
- Creates objects from the array
- Done!
My question to you is: Is this the way to do it, or is there some better way? Am I missing something obvious? Is this the way to do it the "oop" way?
Trying to learn this, so all constructive feedback is welcome!
class Host {
public $ipv4, $mac;
public function __construct($ipv4, $mac){
$this->ipv4 = $ipv4;
$this->mac = $mac;
}
}
class PagesController extends Controller
{
public function index(){
// Get xml with hosts that are connected to local network
$xml = file_get_contents("C:/test.xml");
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// put all hosts in separate array (to make it easier to work with when creating an object further down)
foreach ($array['host'] as $hostKey => $hostValue) {
foreach ($hostValue['address'] as $addressKey => $addressValue) {
foreach ($addressValue['@attributes'] as $attributeKey => $attributeValue) {
$hosts['host'.$hostKey]['address'.$addressKey][$attributeKey] = $attributeValue;
}
}
}
// Create object from array created earlier
foreach ($hosts as $hostKey => $hostValue) {
if(isset($hostValue)){
foreach ($hostValue as $addressKey => $addressValue) {
if(isset($addressValue)){
if(isset($addressValue['addrtype'])){
// ipv4 ?
if($addressValue['addrtype'] == 'ipv4'){
$ipv4 = $addressValue['addr'];
// or mac-address?
} elseif($addressValue['addrtype'] == 'mac'){
$mac = $addressValue['addr'];
}
}
}
}
}
// Skapa ett objekt som heter host+$hostKey
$hosts[$hostKey] = new Host($ipv4, $mac);
}
Note: The reason for the PagesController class and the index() method is cause I'm using the laravel framework.
php object-oriented xml
php object-oriented xml
New contributor
New contributor
edited 2 hours ago
wenzzzel
New contributor
asked 3 hours ago
wenzzzelwenzzzel
12
12
New contributor
New contributor
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago
add a comment |
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
wenzzzel is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211931%2fcreating-objects-from-xml-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
wenzzzel is a new contributor. Be nice, and check out our Code of Conduct.
wenzzzel is a new contributor. Be nice, and check out our Code of Conduct.
wenzzzel is a new contributor. Be nice, and check out our Code of Conduct.
wenzzzel is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211931%2fcreating-objects-from-xml-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Welcome to Code Review! Where does the XML file come from? Is it generated by a service/product? Would it be possible for you to edit your post to include a sample (with names/addresses altered to protect sensitive values, if necessary)?
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
23 mins ago