simplexmlelement array to <xml file format - php

let say my code below is a xml object code
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[delivery_info] => SimpleXMLElement Object
(
[carriage_list] => SimpleXMLElement Object
(
[carriage] => SimpleXMLElement Object
(
[name] => aa
[price] => 0.00
)
)
now need convert it to example below
<?xml version='1.0'?>
<document>
<data>
<delivery_info>
<carriage_list>
<carriage>
<name>aa</name>
<price>aa</price>
</carriage>
</carriage_list>
</delivery_info>
</data>
</document>
XML;
so how i do it? i got search many time already ,no any answer to me
pls help thanks

You can use asXML($filename) function of SimpleXMLElement or cast type to string if you don't need to save to file:
$xml = (string) $element;

Related

php simplexml_load_file and CDATA has data missing completely

I have the following xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<item name="general.global.Event"><![CDATA[EVENT!]]></item>
<item name="general.global.CompanyName"><![CDATA[some name]]></item>
<item name="general.global.CompanyImprint"><![CDATA[Legal information]]></item>
</data>
and my code is as follows
$xml = simplexml_load_file("general.xml") or die("Error: Cannot create object");
print_r($xml);
and my output is missing the CDATA.. how?
SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => general.global.Event
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => general.global.CompanyName
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => general.global.CompanyImprint
)
)
)
)
Text nodes are not exposed with print_r.
You can see the data there is you look at it explicitly:
print $xml->item[0];
The CDATA is being read, read this answer and you'll see that if you print_r($xml->asXML()); The parser recompiles the CDATA information just fine.
For some reason, PHP's var_dump and print_r don't have accurate representation of XML objects. Try this and you can still access the data:
foreach ($xml->item as $item) {
if ('general.global.CompanyImprint' === (string)$item['name']) {
var_dump((string)$item);
}
}
// prints
string(17) "Legal information"

Convert XML to object in PHP a update values of some node and again convert object to XML?

I am dealing with multiple APIs. Please find some steps.
Step1 I have got the first API's result as below.
Let's say I have got the following response.
$xml = '<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response
xmlns="http://schemas.martin-group.com/test">
<Result>
<![CDATA[
<AccountInfo><CusAccount Action="Add" AccountID="2046369" DesiredCycleID="0" Name=""
Corporation="" Cycle=""><AccountType><CusAccountType AccountTypeID="100001" Version="1"
AccountTypeCode="BUS " AccountType="Business" MonthlyBillingMethod="P" /></AccountType>
<InvoiceFormat><ICInvoiceFormat InvoiceFormatID="1" UserID="10490655"
AddressAdjustmentY="0" AddressWidth="3500"/></InvoiceFormat></CusAccount>
</AccountInfo>]]>
</Result>
</Response>
</soap:Body>
</soap:Envelope>';
Step2 I have converted XML to XML Object.
$soap = simplexml_load_string($xml);
$soap->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$object = $soap->xpath('//soap:Envelope/soap:Body')[0]->Response;
$cdata = $object->Result;
$cdata_as_xml = simplexml_load_string($cdata);
Step3 I have updated some node's values as follows.
1. $cdata_as_xml->CusAccount['Action'] = 'Edit'
2. $cdata_as_xml->CusAccount['DesiredCycleID'] = '1'
3. $cdata_as_xml->CusAccount->AccountType->CusAccountType['AccountTypeID'] = '1000023'
Step4 Now I need this updated XML to pass in another API as body as follows:
$xml = '<CusAccount Action="Edit" AccountID="2046369" DesiredCycleID="1" Name=""
Corporation="" Cycle=""><AccountType><CusAccountType AccountTypeID="1000023" Version="1"
AccountTypeCode="BUS " AccountType="Business" MonthlyBillingMethod="P" /></AccountType>
<InvoiceFormat><ICInvoiceFormat InvoiceFormatID="1" UserID="10490655"
AddressAdjustmentY="0" AddressWidth="3500"/></InvoiceFormat></CusAccount>';
But $cdata_as_xml something look like as follows:
SimpleXMLElement Object (
[CusAccount] => SimpleXMLElement Object
( [#attributes] => Array ( [Action] => Edit [AccountID] => 2046414 [DesiredCycleID] => 1
[Name] => [Corporation] => [Cycle] => )
[AccountType] => SimpleXMLElement Object
( [CusAccountType] => SimpleXMLElement Object (
[#attributes] => Array ( [AccountTypeID] => 1000023 [Version] => 1
[ModifyDate] => 5/21/2009 9:46:00 AM [UserID] => 10120452
[AccountTypeCode] => BUS [AccountType] => Business
[MonthlyBillingMethod] => P
)
)
)
[InvoiceFormat] => SimpleXMLElement Object
( [ICInvoiceFormat] => SimpleXMLElement Object
( [#attributes] => Array ( [InvoiceFormatID] => 1 [UserID] => 10490655
[AddressAdjustmentY] => 0 [AddressWidth] => 3500 )
)
)
)
)
How can I get pure XML, not XML Object?
Any help on this would be appreciated.
Thank you.
Use asXML() to save XML into a file
https://www.php.net/manual/en/simplexmlelement.asxml.php
If you don't specify a filename, the method will return a string, containing the well-formed XML of the Element.

PHP - get values from xml

I have a some problem with get value from xml.
XML look like
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://crd.gov.pl/xml/schematy/UPO/2008/05/09/UPO.xsl"?>
<pos:Document xmlns:pos="SOMEURL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<pos:DescribeDoc/>
<pos:UPD>
<pos:IdDoc>procotol-UPD2198338</pos:IdDoc>
<pos:IdCases>221872</pos:IdCases>
<pos:additionalInfo TypeInfo="Source">Some string</pos:additionalInfo>
</pos:UPD>
...
I general try to get to pos:IdCases.
I try this code:
$domContent = new SimpleXMLElement(((string) $content), LIBXML_COMPACT);
$test = $domContent->xpath('/pos:Document/pos:UPD/*');
foreach($test as $node){
print_r($node)
}
I get a some object such as
SimpleXMLElement Object
(
[0] => procotol-UPD2198338
)
SimpleXMLElement Object
(
[0] => 221872
)
SimpleXMLElement Object
(
[#attributes] => Array
(
[TypeInfo] => Source
)
[0] => Some string
)
But I must get to pos:IdCases. I can't use index [1] because order can change.
My question is:
How can I get to value in node: pos:IdCases
I can't add id or another info to node because this xml was signed (XADES).
Can you give me some advice? Thanks for help
Simply change the XPath to match the <Pos:IdCases/> node:
$test = $domContent->xpath('/pos:Document/pos:UPD/pos:IdCases');

How to get an element from simplexml [duplicate]

This question already has answers here:
SimpleXML get node value
(3 answers)
Closed 8 years ago.
SimpleXMLElement Object
(
[SMSMessage] => SimpleXMLElement Object
(
[Sid] => xyz
[DateUpdated] => 2013-05-02 18:43:19
[DateCreated] => 2013-05-02 18:43:19
[DateSent] => 1970-01-01 05:30:00
[AccountSid] => xx
[To] => 09011148771
[From] => xx
[Body] => Hello
[BodyIndex] => SimpleXMLElement Object
(
)
[Status] => sending
[Direction] => outbound-api
[Price] => SimpleXMLElement Object
(
)
[ApiVersion] => SimpleXMLElement Object
(
)
[Uri] => /v1/Accounts/xx/Sms/Messages/xyz
)
)
I tried:
$xml->Sid;
But it returns SimpleXMLElement Object ( )
I also tried $xml->title, which also returned the same SimpleXMLElement Object ( )
How to get the Sid from the above XMl
I've been Fiddling a bit and recreated a structure similar to yours:
$string='<?xml version="1.0"?>
<xml>
<SMSMessage>
<Sid>xyz</Sid>
<DateUpdated>2013-05-02 18:43:19</DateUpdated>
</SMSMessage>
</xml>';
$xml = new SimpleXMLElement($string);
print_r($xml);
this outputs:
SimpleXMLElement Object
(
[SMSMessage] => SimpleXMLElement Object
(
[Sid] => xyz
[DateUpdated] => 2013-05-02 18:43:19
)
)
Which is equal to yours. And I could print xyz doing:
echo $xml->SMSMessage->Sid;
Try it out, you might be missing some parent node or something.
Your Method: To do it the way you want, I think you'd have to go one step further with your call since the SimpleXMLObject is parent to another SimpleXMLObject. E.g. $xml->SMSMessage->Sid;. I generally recommend using xpath with XML because in most cases, you want to jump directly to a specific node and not traverse the whole XML tree. For example, this: $xml->xpath('//[node]') is quicker than $xml->tier1->tier2->tier3->etc.
Preferred Method: Assuming $xml represents the SimpleXMLObject you've posted, you can access Sid like this: $xml->xpath('//Sid');. This should skip directly to the "Sid" node in the tree.
In simplexml you always have to cast your values - so you have to do this:
echo $xml->Sid;
(echo automatically casts). Or explicitly:
$string = (string) $xml->id;
if you have:
$string='<?xml version="1.0"?>
<xml>
<SMSMessage>
<Sid>xyz</Sid>
<DateUpdated>2013-05-02 18:43:19</DateUpdated>
</SMSMessage>
</xml>';
$xml = new SimpleXMLElement($string);
print_r('<pre>');
$Sid = (array)($xml->SMSMessage->Sid);
print_r($Sid[0]);
print_r($xml);
You can access Sid like so $Sid = (array)(xml->SMSMessage->Sid); echo $Sid[0];
But if you rather use array you can do this:
$string='<?xml version="1.0"?>
<xml>
<SMSMessage>
<Sid>xyz</Sid>
<DateUpdated>2013-05-02 18:43:19</DateUpdated>
</SMSMessage>
</xml>';
$array= json_decode(json_encode(new SimpleXMLElement($string)), true);
print_r('<pre>');
print_r($array['SMSMessage']['Sid']);
print_r($array);

Could not to read data from xml file due to that tag <![CDATA[]]

test.xml
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.2/"
>
<item>
<title>Hello world!</title>
<link>http://localhost/wordpress/?p=1</link>
<category><![CDATA[Uncategorized]]></category>
<HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>
</item>
</rss>
i used that code read xml file
<?php
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
echo "<pre>";
print_r($xml);
echo "</pre>";
} else {
exit('Failed to open test.xml.');
}
?>
Output
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
)
[item] => SimpleXMLElement Object
(
[title] => Hello world!
[link] => http://localhost/wordpress/?p=1
[category] => SimpleXMLElement Object
(
)
[HEADLINE] => SimpleXMLElement Object
(
)
)
)
but i have problem with those tag in xml
<category><![CDATA[Uncategorized]]></category>
<HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>
to read data bcoz of it <![CDATA[]] in category and HEADLINE
i need output
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
)
[item] => SimpleXMLElement Object
(
[title] => Hello world!
[link] => http://localhost/wordpress/?p=1
[category] => Uncategorized
[HEADLINE] => Does Sleep Apnea Offer Some Protection During Heart Attack?
)
)
Try this when loading the file
$xml = simplexml_load_file($this->filename,'SimpleXMLElement', LIBXML_NOCDATA);
Try simply this:
var_dump(
(string)$xml->item->category,
(string)$xml->item->HEADLINE
);
It's worth nothing that SimpleXML builds stuff on the fly so print_r() on objects does not necessarily display useful info.
try casting the
[category] => SimpleXMLElement Object
(
)
[HEADLINE] => SimpleXMLElement Object
(
)
to strings, and i think you will get what you need
$item['category']=(string)$item['category'];

Categories