我可以使用什么语言来组合多个
XML文件.多个10个文件.
PHP,java还是什么?
我试图使用XSLT,但我不知道我是否需要像Saxon这样的“处理器”.
文档令人困惑,因为我不知道从哪里开始.
总而言之,我需要有人指出我正确的方向.
有人请帮忙.我一直在努力解决这个问题
<xml version="1.0"> <products> <price>Price List Here</price> <model>Model Number Here</model> </product>
解决方法
您可以使用任何允许您直接操作xml的语言.我建议用DOM而不是SAX找东西.如果你使用SAX,你必须自己基本遍历xml – 根据我的经验,这是一个皮塔饼. DOM允许您以更多OOP方式对xml执行操作.
立即产生的东西将是围绕xml“文档”的包装xml.
所以类似于:
<documents> <document> <!-- Your xml here --> </document> <document> <!-- Your xml here --> </document> <document> <!-- Your xml here --> </document> </documents>
伪代码将是:
创建文档根目录.
添加一个名为documents的元素,将其用作root.
迭代每个xml文件.
对于每个文件,创建一个名为document的新元素.将该元素添加到父元素.从文件加载xml.将该节点导入外部文档.将导入的节点附加到文档元素子集合中.
编辑
正如这里所承诺的那样,经过测试的更新代码我知道有效:
<?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(); ?>