Creating objects from xml-data












0












$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:




  1. Loading the xml

  2. Encoding it into json

  3. Making an array from the json

  4. Making a simpler array from the last array

  5. Creates objects from the array

  6. 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.










share|improve this question









New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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ᴇᴌᴀ
    5 hours ago
















0












$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:




  1. Loading the xml

  2. Encoding it into json

  3. Making an array from the json

  4. Making a simpler array from the last array

  5. Creates objects from the array

  6. 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.










share|improve this question









New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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ᴇᴌᴀ
    5 hours ago














0












0








0


1



$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:




  1. Loading the xml

  2. Encoding it into json

  3. Making an array from the json

  4. Making a simpler array from the last array

  5. Creates objects from the array

  6. 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.










share|improve this question









New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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:




  1. Loading the xml

  2. Encoding it into json

  3. Making an array from the json

  4. Making a simpler array from the last array

  5. Creates objects from the array

  6. 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






share|improve this question









New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 7 hours ago







wenzzzel













New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 8 hours ago









wenzzzelwenzzzel

12




12




New contributor




wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






wenzzzel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $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ᴇᴌᴀ
    5 hours 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ᴇᴌᴀ
    5 hours 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ᴇᴌᴀ
5 hours 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ᴇᴌᴀ
5 hours ago










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.










draft saved

draft discarded


















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.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀