我试图用我从大型XML文件解析的数据来构建关联数组。问题是数组只输出一个循环。我无法使用所有相关节点循环来构建它。
XML结构如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
...
<property>
...
<images>
<image id="1">
<url>https://example.com/image1.jpg</url>
<title>
<en>en_lang_translation</en>
<es>es_lang_translation</es>
<de>de_lang_translation</de>
</title>
</image>
<image id="2">
<url>https://example.com/image2.jpg</url>
<title>
<en>en_lang_translation</en>
<es>es_lang_translation</es>
<de>de_lang_translation</de>
</title>
</image>
</images>
...
</property>
...
</root>
...到目前为止我所获得的代码如下:
foreach($property->childNodes as $nodename) {
if($nodename->nodeName == 'images') {
$imageArray = array();
foreach($nodename->childNodes as $imageNodes) {
foreach($imageNodes->childNodes as $imageSubNodes) {
if($imageSubNodes->nodeName == 'url') {
$imageArray['url'] = $imageSubNodes->nodeValue;
} else if($imageSubNodes->nodeName == 'title') {
foreach($imageSubNodes->childNodes as $titleNode) {
foreach($titleNode->childNodes as $titleLangNode) {
$childs = $titleNode->childNodes;
$key = $titleNode->nodeName;
foreach ($childs as $p) {
$imageArray[$key] = $p->nodeValue;
}
}
}
}
}
}
}
}
指导表示赞赏。感谢。
答案 0 :(得分:0)
破解了。下面的评论(谢谢你们)指出了我的解决方案。我在每次循环运行时都覆盖了数组。我回到绘图板并简化了foreach循环,因为那是我出错的地方,在多个循环中迷路了。我的固定代码如下:
while(is_object($property = $doc->getElementsByTagName("property")->item($i))) {
foreach($property->childNodes as $nodename) {
if($nodename->nodeName=='images') {
foreach($nodename->childNodes as $images) {
if($images->nodeName == 'image') {
//now in each images node - children are image
foreach($images->childNodes as $image) {
//now in each image node - children are url & title
if($image->nodeName == 'url'){
$image_url = $image->nodeValue;
}
if($image->nodeName == 'title'){
foreach($image->childNodes as $title) {
// now in title node - children en, es, de
if($title->nodeName == 'en'){
$title_en = $title->nodeValue;
}
if($title->nodeName == 'es'){
$title_es = $title->nodeValue;
}
if($title->nodeName == 'de'){
$title_de = $title->nodeValue;
}
}
}
}
}
$imageArray[] = array('url' => $image_url, 'title_en' => $title_en, 'title_es' => $title_es, 'title_de' => $title_de);
}
}
}
$i++;
}
echo '<ul>';
foreach ($imageArray as $key => $value) {
echo '<li>'.$value['url'].' - '.$value['title_en'].'</li>';
}
echo '</ul>';
答案 1 :(得分:0)
或者,考虑使用零循环,if
逻辑或数组,仅XSLT将源XML转换为HTML列表项:
XSLT (另存为.xsl脚本,格式正确的.xml文件)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<ul>
<xsl:apply-templates select="property"/>
</ul>
</xsl:template>
<xsl:template match="property|images">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="image">
<li><xsl:value-of select="concat(url, ' - ', title/en)"/></li>
<li><xsl:value-of select="concat(url, ' - ', title/es)"/></li>
<li><xsl:value-of select="concat(url, ' - ', title/de)"/></li>
</xsl:template>
</xsl:stylesheet>
<强> PHP 强>
// LOAD XML AND XSL SOURCES
$xml = new DOMDocument;
$xml->load('Input.xml');
$xsl = new DOMDocument;
$xsl->load('XSLTScript.xsl');
// CONFIGURE TRANSFORMER
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// TRANSFORM XML
$newXML = new DOMDocument;
$newXML = $proc->transformToXML($xml);
echo $newXML; // <--- STRING OUTPUT
// <ul>
// <li>https://example.com/image1.jpg - en_lang_translation</li>
// <li>https://example.com/image1.jpg - es_lang_translation</li>
// <li>https://example.com/image1.jpg - de_lang_translation</li>
// <li>https://example.com/image2.jpg - en_lang_translation</li>
// <li>https://example.com/image2.jpg - es_lang_translation</li>
// <li>https://example.com/image2.jpg - de_lang_translation</li>
// </ul>