Solved

How can i generate these XML with SimpleXML()

Posted on 2016-09-08
5
74 Views
Last Modified: 2016-09-13
How can I these Generate with SimpleXML()?
<?xml version="1.0"?>
  <itwresponse version="1.0">
    <field name="code">
      <string>OK</string>
    </field>
    <field name="output">
      <array>
        <object>
          <field name="ent">
            <string>Text</string>
          </field>
          <field name="expires">
            <string>232432432432423</string>
          </field>
        </object>
      </array>
    </field>
  </itwresponse>
0
Comment
Question by:obcc-srs
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41790837
Got a "neglected" alert on this question.  Where does the raw data come from?  A good test data set is the first thing we need in order to show you how to assemble this XML document.  Thanks.
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 41790901
To generate SimpleXMLElement object from xml data, you simply pass it to the class constructor:
$xmlStr = ''; // Your xml string will be here
$xml = SimpleXMLElement($xmlStr);

Open in new window


So in your case, you can do something like the following:
$xmlStr = '<?xml version="1.0"?>
  <itwresponse version="1.0">
    <field name="code">
      <string>OK</string>
    </field>
    <field name="output">
      <array>
        <object>
          <field name="ent">
            <string>Text</string>
          </field>
          <field name="expires">
            <string>232432432432423</string>
          </field>
        </object>
      </array>
    </field>
  </itwresponse>';

$xml = new SimpleXMLElement($xmlStr);

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41790913
If you're new to PHP and want to learn, this article can help you get started with a good foundation.

If you already have this XML document (from whatever source) and want to find information inside it (called "parsing the XML"), this code example should help you get started.  If you have some information you want to put into an XML document, then we need to see the test data set in order to show you some ways of assembling the XML document.  

In any case, the XML tag names, as shown in the original post, are likely to be confusing.  You apparently have field name  tags nested inside field name  tags -- this is not a very strong semantic design!  So if this XML document is one you are producing to send to another application, you might want to refine the meaning and clarify the names of your information elements.

If you're not "stuck" with XML, JSON is almost always a better alternative.

Please see: https://iconoun.com/demo/temp_obcc.php
<?php // demo/temp_obcc.php
/**
 * https://www.experts-exchange.com/questions/28968528/How-can-i-generate-these-XML-with-SimpleXML.html
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


// TEST DATA FROM THE POST AT E-E
$xml = <<<EOX
<?xml version="1.0"?>
  <itwresponse version="1.0">
    <field name="code">
      <string>OK</string>
    </field>
    <field name="output">
      <array>
        <object>
          <field name="ent">
            <string>Text</string>
          </field>
          <field name="expires">
            <string>232432432432423</string>
          </field>
        </object>
      </array>
    </field>
  </itwresponse>
EOX;


// MAKE AN OBJECT AND FIND INFORMATION INSIDE THE OBJECT
$obj  = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

$code = (string)$obj->field[0]->string;
var_dump($code);

$expr = (string)$obj->field[1]->array->object->field[1]->string;
var_dump($expr);

Open in new window

Outputs:
string(2) "OK"
string(15) "232432432432423"

Open in new window

0
 

Author Closing Comment

by:obcc-srs
ID: 41795584
The question was answered
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
google map tracking website 5 24
how to use a switch statement with heredoc 11 23
How do I fix this UPDATE error? 7 24
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question