我如何使用此元素生成此 XML 格式
how can i generate this XML format with this element
输出应该是这样的:
<invoice:company this="1">
<invoice:transport from="7777777777" to="77777777777">
<invoice:via via="7777777777" id="1"/>
</invoice:transport>
</invoice:company>
但我得到这个:
<company this="1">
<transport from="7777777777" to="77777777777">
<via via="7777777777" id="1"/>
</transport>
</company>
我将其用作 XML 生成器:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><invoice>
</invoice>');
//child of invoice
$company= $xml->addChild('company');
//child of company
$transport = $processing->addChild('transport');
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via');
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
$xml->saveXML();
$xml->asXML("company_001.xml");'
为什么元素标签上有“:”?我怎样才能做到这一点?我也需要那个。
如评论中所述,invoice:
是文档中元素的命名空间。
创建带有命名空间的XML文档时,您需要声明它。在下面的代码中,我已经在加载到 SimpleXMLElement
的初始文档中完成了它。我不知道这个命名空间的正确定义 - 所以我一直使用 "http://some.url"
(并且所有引用都需要更改)。如果你没有定义这个命名空间,SimpleXML 会在你第一次使用它时添加它自己的定义。
添加元素时,可以定义添加到哪个命名空间,addChild
的第三个参数是命名空间。
所以...
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
</invoice>');
//child of invoice
$processing= $xml->addChild('company', "", "http://some.url");
//child of company
$transport = $processing->addChild('transport', "", "http://some.url");
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via', "", "http://some.url");
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
echo $xml->asXML();
生成(我已格式化输出以提供帮助)...
<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
<invoice:company>
<invoice:transport to="77777777777" from="77777777777">
<invoice:via id="1" via="77777777777" />
</invoice:transport>
</invoice:company>
</invoice>
由于我不确定这是否是您创建的整个文档,因此可能需要进行一些小的更改,但希望这对您有所帮助。
输出应该是这样的:
<invoice:company this="1">
<invoice:transport from="7777777777" to="77777777777">
<invoice:via via="7777777777" id="1"/>
</invoice:transport>
</invoice:company>
但我得到这个:
<company this="1">
<transport from="7777777777" to="77777777777">
<via via="7777777777" id="1"/>
</transport>
</company>
我将其用作 XML 生成器:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><invoice>
</invoice>');
//child of invoice
$company= $xml->addChild('company');
//child of company
$transport = $processing->addChild('transport');
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via');
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
$xml->saveXML();
$xml->asXML("company_001.xml");'
为什么元素标签上有“:”?我怎样才能做到这一点?我也需要那个。
如评论中所述,invoice:
是文档中元素的命名空间。
创建带有命名空间的XML文档时,您需要声明它。在下面的代码中,我已经在加载到 SimpleXMLElement
的初始文档中完成了它。我不知道这个命名空间的正确定义 - 所以我一直使用 "http://some.url"
(并且所有引用都需要更改)。如果你没有定义这个命名空间,SimpleXML 会在你第一次使用它时添加它自己的定义。
添加元素时,可以定义添加到哪个命名空间,addChild
的第三个参数是命名空间。
所以...
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
</invoice>');
//child of invoice
$processing= $xml->addChild('company', "", "http://some.url");
//child of company
$transport = $processing->addChild('transport', "", "http://some.url");
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via', "", "http://some.url");
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
echo $xml->asXML();
生成(我已格式化输出以提供帮助)...
<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
<invoice:company>
<invoice:transport to="77777777777" from="77777777777">
<invoice:via id="1" via="77777777777" />
</invoice:transport>
</invoice:company>
</invoice>
由于我不确定这是否是您创建的整个文档,因此可能需要进行一些小的更改,但希望这对您有所帮助。