Exemplo n.º 1
0
/**
 *添加信息到xml文件
 */
function addToXML($filename, $brand, $title, $url)
{
    //处理产品名字符乱码
    $brand = strReplaceToEntity($brand);
    $title = strReplaceToEntity($title);
    $url = strReplaceToEntity($url);
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    if ($xml->load($filename)) {
        $root = $xml->documentElement;
        //获得根节点(root)
        $productXml = $xml->createElement("product");
        $titleXml = $xml->createElement("title", $title);
        $brandXml = $xml->createElement("brand", $brand);
        $urlXml = $xml->createElement("url", $url);
        $productXml->appendChild($titleXml);
        $productXml->appendChild($brandXml);
        $productXml->appendChild($urlXml);
        $root->appendChild($productXml);
        $xml->appendChild($root);
        //生成XML
        $xml->save($filename);
    } else {
        echo 'xml 文件 ' . $filename . '更新错误! <br/>';
    }
}
Exemplo n.º 2
0
/**
 *添加信息到xml文件
 */
function addToXML($xmlName, $brand, $title, $url)
{
    //处理文件名
    $xmlName = strtolower(strReplarceToNull($xmlName));
    //处理产品名字符乱码
    $brand = strReplaceToEntity($brand);
    $title = strReplaceToEntity($title);
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    $filename = 'xml/' . $xmlName . '.xml';
    if ($xml->load($filename)) {
        $root = $xml->documentElement;
        //获得根节点(root)
        $productXml = $xml->createElement("product");
        // $idXml = $xml->createAttribute("id");
        // $idXml->value = 4;
        $titleXml = $xml->createElement("title", $title);
        $brandXml = $xml->createElement("brand", $brand);
        $urlXml = $xml->createElement("url", $url);
        // $productXml->appendChild($idXml);
        $productXml->appendChild($titleXml);
        $productXml->appendChild($brandXml);
        $productXml->appendChild($urlXml);
        $root->appendChild($productXml);
        $xml->appendChild($root);
        //生成XML
        $xml->save($filename);
    } else {
        echo 'xml 文件 ' . $xmlName . '更新错误! <br/>';
    }
}