gpt4 book ai didi

php - 在 PHP 中从 XML 值创建 XML?

转载 作者:行者123 更新时间:2023-12-04 20:15:08 25 4
gpt4 key购买 nike

我正在从一个字符串创建一个 XML(这个字符串是另一个 XML 的值)。

我试过simplexml_load_string去创造。

我的场景:

我的 UI 在 php 中,我通过 PHPExcel 发送 Excel 工作表数据到我项目的服务部分。它返回:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<root>
<searcheddata>
{Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0}, Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}
</searcheddata>
</root>

现在我尝试从 <searcheddata> 的值创建 XML .

Atlast 我需要一个 XML:
<xml>
<root>
<Account0>
<AccountName>1</AccountName>
<AddressLine2>0.0</AddressLine2>
<Country>0.0</Country>
<CellPhone>0.0</CellPhone>
<City>0.0</City>
<WorkPhone>0.0</WorkPhone>
<Other>0.0</Other>
<HomePhone>0.0</HomePhone>
<AddressLine1>0.0</AddressLine1>
<State>0.0</State>
<Zip>0.0</Zip>
<AlternateEmail>0.0</AlternateEmail>
</Account0>
<City1>
<City>1</City>
<AddressLine2>0.0</AddressLine2>
<Country>0.0</Country>
<CellPhone>0.0</CellPhone>
<WorkPhone>0.0</WorkPhone>
<Other>0.0</Other>
<HomePhone>0.0</HomePhone>
<AddressLine1>0.0</AddressLine1>
<State>0.0</State>
<Zip>0.0</Zip>
<AlternateEmail>0.0</AlternateEmail>
</City1>
</root>
</xml>

我尝试了什么:
<?php

$str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root><searcheddata>{Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0}, Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}</searcheddata></root>";

$a = array();
$name = array();
$values = array();
$mainArray = array();
$xml = simplexml_load_string($str);
$s3 = $xml->searcheddata;
$pieces = (explode("},", $s3));
$pieces[0] = substr($pieces[0], 1);
$length = sizeof($pieces);
$pieces[$length - 1] = substr($pieces[$length - 1], 0, -2);
foreach ($pieces as $value) {
$morePiece = (explode("={", $value));
for ($i = 0; $i < sizeof($morePiece); $i = $i + 2) {
$lastPiece = explode(", ", $morePiece[$i]);
for ($j = 0; $j < sizeof($lastPiece); $j++) {
array_push($a, $lastPiece[0]);
}
}
for ($i = 1; $i < sizeof($morePiece); $i = $i + 2) {
$lastPiece2 = explode(", ", $morePiece[$i]);
for ($j = 0; $j < sizeof($lastPiece2); $j++) {
$finalPiece2 = explode("=", $lastPiece2[$j]);
for ($k = 0; $k < sizeof($finalPiece2); $k++) {
if ($k % 2 == 0) {
array_push($name, $finalPiece2[$k]);
} else {
array_push($values, $finalPiece2[$k]);
}
}
}
}
}
echo sizeof($pieces)."<br>";
print_r($a);
echo '<br><br>';
print_r($name);
echo '<br><br>';
print_r($values);
echo '<br><br>';
?>

我试图将我所有的键和值放在各自的数组中,然后使用 simplexml-load-string 创建一个 XML .

欢迎提出建议。

最佳答案

尽量避免编写完整的解析器,下面结合了一些正则表达式和不同格式(字符串、JSON、数组、SimpleXML 和 DOMDocument)的转换来达到预期的结果。

我必须管理它有点骇人听闻 - 例如,当前的一个限制是,根据您的示例,值必须是数字(例如 123 或 123.45) - 但在您的情况下可能就足够了。

$source =<<<XML
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<root>
<searcheddata>
{Account0={AccountName=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, City1={City=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}, State2={State=1, AddressLine2=0.0, Country=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, Zip=0.0, AlternateEmail=0.0}, Country3={Country=1, AddressLine2=0.0, CellPhone=0.0, City=0.0, WorkPhone=0.0, Other=0.0, HomePhone=0.0, Email=0.0, AddressLine1=0.0, State=0.0, Zip=0.0, AlternateEmail=0.0}}
</searcheddata>
</root>
XML;

// Get rid of backslashes
$source = stripslashes($source);

// Create XML object, grab the data and remove whitespace
$xml = new SimpleXMLElement($source);
$data = (string)$xml->searcheddata;
$data = preg_replace('/\s/', '', $data);

// Turn data into JSON:
// {foo={bar=1, baz=2.34}} => {"foo":{"bar":"1","baz":"2.34"}}
$data = preg_replace('/(\w+)=([\d\.]+)/', '"\1":"\2"', $data);
$data = preg_replace('/(\w+)=/', '"\1":', $data);

// Turn JSON into associative array
$decodedJson = json_decode($data, true);

// Function for adding array elements as XML child elements to XML element
function addElements(SimpleXMLElement $element, $a)
{
foreach ($a as $key => $value) {
if (is_array($value)) {
$child = $element->addChild($key);
addElements($child, $value);
} else
$element->addChild($key, $value);
}
}

// Turn array into another Simple XML element and create <xml> and <root> tags
$simpleXml = new SimpleXMLElement('<xml/>');
$root = $simpleXml->addChild('root');
addElements($root, $decodedJson);

// Pretty output by converting SimpleXML object to DOMElement
$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();

输出:
<?xml version="1.0"?>
<xml>
<root>
<Account0>
<AccountName>1</AccountName>
<AddressLine2>0.0</AddressLine2>
<Country>0.0</Country>
<CellPhone>0.0</CellPhone>
<City>0.0</City>
<WorkPhone>0.0</WorkPhone>
<Other>0.0</Other>
<HomePhone>0.0</HomePhone>
<Email>0.0</Email>
<AddressLine1>0.0</AddressLine1>
<State>0.0</State>
<Zip>0.0</Zip>
<AlternateEmail>0.0</AlternateEmail>
</Account0>
<City1>
<City>1</City>
<AddressLine2>0.0</AddressLine2>
<Country>0.0</Country>
<CellPhone>0.0</CellPhone>
<WorkPhone>0.0</WorkPhone>
<Other>0.0</Other>
<HomePhone>0.0</HomePhone>
<Email>0.0</Email>
<AddressLine1>0.0</AddressLine1>
<State>0.0</State>
<Zip>0.0</Zip>
<AlternateEmail>0.0</AlternateEmail>
</City1>
...
</root>
</xml>

关于php - 在 PHP 中从 XML 值创建 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com