Solved

How can i generate these XML with SimpleXML()

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

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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSL - change date format 3 29
c# code 19 59
Adding through query php 9 11
two tables one button 11 16
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now