Solved

How can i generate these XML with SimpleXML()

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to create a scatter graph with straight lines 6 25
WordPress Themes 10 34
xampp tool 12 23
array_values - reorder after unset? 5 0
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now