Solved

How can i generate these XML with SimpleXML()

Posted on 2016-09-08
5
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 110

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 110

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 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