在Python或PHP中从Treenode创建XML

2024-10-04 11:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下的xml

<?xml version="1.0"?>
<rss version="2.0">
<channel>
    <title>client</title>
    <link>http://www.client.com</link>
    <description>gdfgdfgdg</description>
<item>
   <id>N10987281A</id>
    <title>dfgdfgdfgdfgdg  </title>
    <link>https://www.client.com/p</link>
    <image_link>https://client.com/t_desktop-pdp-v1/v1505732008/N10987281A_1.jpg</image_link>
    <condition>new</condition>
    <availability>in stock</availability>
    <price>31.50 AED</price>
    <gtin>27242852310</gtin>
    <brand>dfgdgdfg</brand>
    <mpn>CDR 50 Shrink</mpn>
    <custom_label_2>audio_video</custom_label_2>
    <description>fgdfgdfgdfg rgdfgdgdfgdfgdfgdg</description>
    <product_type>Electronics &amp; Mobiles/Television &amp; Video</product_type>
    <google_product_category>Electronics</google_product_category>
    <shipping>
        <price>0.00 AED</price>
        <country>AE</country>
        <service>Standard</service>
    </shipping>
</item>
</channel>
</rss>

对于上面的XML,我有下面这样的树节点和从这个getTreenode of the xml得到的树节点

/rss    
/rss@version    2.0
/rss/channel    
/rss/channel/title  client
/rss/channel/link   http://www.client.com
/rss/channel/description    gdfgdfgdg
/rss/channel/item   
/rss/channel/item/id    N10987281A
/rss/channel/item/title dfgdfgdfgdfgdg
/rss/channel/item/link  https://www.client.com/p
/rss/channel/item/image_link    https://client.com/t_desktop-pdp-v1/v1505732008/N10987281A_1.jpg
/rss/channel/item/condition new
/rss/channel/item/availability  in stock
/rss/channel/item/price 31.50 AED
/rss/channel/item/gtin  27242852310
/rss/channel/item/brand dfgdgdfg
/rss/channel/item/mpn   CDR 50 Shrink
/rss/channel/item/custom_label_2    audio_video
/rss/channel/item/description   fgdfgdfgdfg rgdfgdgdfgdfgdfgdg
/rss/channel/item/product_type  Electronics & Mobiles/Television & Video
/rss/channel/item/google_product_category   Electronics
 /rss/channel/item/shipping 
 /rss/channel/item/shipping/price   0.00 AED
 /rss/channel/item/shipping/country AE
 /rss/channel/item/shipping/service Standard

现在,如何从上面的树节点本身创建XML

有人能帮我吗

提前谢谢


Tags: httpscomclienttitlewwwchannellinkdescription
1条回答
网友
1楼 · 发布于 2024-10-04 11:25:38

我添加了评论,因为这比尝试添加完整的描述更容易。你知道吗

但它基本上是从文件中读取树,在解析每一行时创建或移动到各个级别。如果标签中有一个@,那么这意味着创建一个属性。。。你知道吗

$file = file("a.txt", FILE_IGNORE_NEW_LINES);
// Extract root node to create new document
$firstLine = array_shift($file);
$xml = new SimpleXMLElement("<" . substr($firstLine,1) . " />");

foreach ( $file as $line ) {
    // Set start insert point to root node
    $xmlC = $xml;
    $matches = [];
    // Split into tag name and content parts
    preg_match("/(.*?)\s+(.*)/", $line, $matches);
    // Split levels of tag name apart
    $tag = explode("/", substr($matches[1], 1));
    // Root node is already there
    $roottag = array_shift($tag);
    // Check if adding an attribute to root node
    $element = explode("@", $roottag);
    if ( isset($element[1]) )   {
        $xmlC->addAttribute($element[1], $matches[2]);
        unset($matches[2]);
    }
    // For each level of tag
    foreach ( $tag as $level )  {
        $element = explode("@", $level);
        // If tag is already set, then just move down a level
        if ( isset($xmlC->{$element[0]}) ) {
            $xmlC = $xmlC->{$element[0]};
        }
        // If not set then add a new element
        else    {
            $xmlC = $xmlC->addChild($element[0]);
        }
        // If an attribute needs to be created
        if ( isset($element[1]) )   {
            $xmlC->addAttribute($element[1], $matches[2]);
            unset($matches[2]);
        }
    }
    // If there is a value, then add it to last node
    if ( isset($matches[2]) ) {
        $xmlC[0] = $matches[2];
    }
}

echo $xml->asXML();

一个稍微微调的循环,它使用正则表达式来提取属性。。。你知道吗

foreach ( $file as $line ) {
    // Set start insert point to root node
    $xmlC = $xml;
    $matches = [];
    // Split into tag name, attribute and content parts
    preg_match("/(.*?)(@.*?)?\s+(.*)/", $line, $matches);

    // Split levels of tag name apart
    $tag = explode("/", substr($matches[1], 1));
    // Root node is already there
    $roottag = array_shift($tag);
    // For each level of tag
    foreach ( $tag as $level )  {
        // If tag is already set, then just move down a level
        if ( isset($xmlC->{$level}) ) {
            $xmlC = $xmlC->{$level};
        }
        // If not set then add a new element
        else    {
            $xmlC = $xmlC->addChild($level);
        }
    }
    // If there is a value, then add it to last node (either as an attribute or text
    if ( !empty($matches[3]) ) {
        if ( !empty($matches[2]) )    {
            $xmlC->addAttribute(substr($matches[2],1), $matches[3]);
        }
        else    {
            $xmlC[0] = $matches[3];
        }
    }
}

相关问题 更多 >