Solved

How can i generate these XML with SimpleXML()

Posted on 2016-09-08
5
80 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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