合并许多XML文件 [英] Combine Many XML Files

查看:109
本文介绍了合并许多XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用哪种语言来组合多个XML文件.多于10个文件.

What language can I use to combine multiple XML files. Multiple as 10+ files.

PHP,java还是什么?

PHP, java, or what?

我尝试使用XSLT,但不知道是否需要像Saxon这样的处理器".

I tried to use XSLT but I do not know if I need a 'processor' such as Saxon.

由于我不知道从哪里开始,文档令人困惑.

The docs were to confusing as I did not know where to start.

总而言之,我需要有人将我指向正确的方向.

All in all, I need someone to point me in the right direction.

请有人帮忙.我已经尝试了好几天了

Someone please help. I've been trying to figure this out for days

<xml version="1.0">
<products>
<price>Price List Here</price>
<model>Model Number Here</model>
</product>

推荐答案

您可以使用允许直接操作xml的任何语言.我建议您使用DOM而不是SAX来找到一些东西.如果使用SAX,则必须基本上自己遍历xml-根据我的经验,这是一个皮塔饼. DOM允许您以更多的OOP方式对xml进行操作.

You can use any language that allows you to manipulate xml directly. I suggest finding something with DOM rather than SAX. If you use SAX you have to basically traverse the xml yourself - a pita in my experience. DOM allows you to act on the xml in a more OOP manner.

立即想到的是XML文档"周围的包装XML.

Something that springs immediately to mind would be a wrapper xml around your xml "documents".

所以像这样:

<documents>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
</documents>

伪代码为: 创建文档根目录. 添加一个称为document的元素,将其用作根. 遍历每个xml文件. 为每个文件创建一个称为document的新元素.将该元素添加到父元素.从文件中加载xml.将该节点导入外部文档.将导入的节点追加到文档元素子集合中.

The pseudo code would be: Create a document root. Add an element called documents, use that as the root. Iterate each of your xml files. For each file create a new element called document. Add that element to the parent. Load the xml from the file. Import that node into the outer document. Append the imported node into the document elements child collection.

编辑 正如这里所承诺的,经过测试的更新代码我知道有效:

EDIT As promised here is the updated code that was tested and I know works:

<?php

    // Replace the strings below with the actual filenames, add or decrease as fit
    $filenames = array(0 => "test.xml", 1 => "test2.xml", 2 => "test3.xml" );

    $docList = new DOMDocument();

    $root = $docList->createElement('documents');
    $docList->appendChild($root);

    foreach($filenames as $filename) {

        $doc = new DOMDocument();
        $doc->load($filename);

        $xmlString = $doc->saveXML($doc->documentElement);

        $xpath = new DOMXPath($doc);
        $query = "//product";  // this is the name of the ROOT element

        $nodelist = $xpath->evaluate($query, $doc->documentElement);

        if( $nodelist->length > 0 ) {

            $node = $docList->importNode($nodelist->item(0), true);

            $xmldownload = $docList->createElement('document');
            $xmldownload->setAttribute("filename", $filename);
            $xmldownload->appendChild($node);

            $root->appendChild($xmldownload);
        }

    }

    echo $docList->saveXML();
?>

这篇关于合并许多XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆