【问题标题】:adding parent to xml doc in perl在perl中将父级添加到xml doc
【发布时间】:2013-03-12 00:42:23
【问题描述】:

我在将父级添加到 xml 文档时遇到问题。我得到了 xml:

<book id="bk104">
  <author>Corets, Eva</author>
  <title>Oberon's Legacy</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2001-03-10</publish_date>
  <description>In post-apocalypse England, the mysterious 
  agent known only as Oberon helps to create a new life 
  for the inhabitants of London. Sequel to Maeve 
  Ascendant.</description>
</book>

我想为这本书添加父标签,所以它是:

<library>
 <book id="bk104">
  <author>Corets, Eva</author>
  <title>Oberon's Legacy</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2001-03-10</publish_date>
  <description>In post-apocalypse England, the mysterious 
  agent known only as Oberon helps to create a new life 
  for the inhabitants of London. Sequel to Maeve 
  Ascendant.</description>
 </book>
</library>

我正在使用XML::LIBXML,我已经尝试获取root权限

my $root = $doc->getDocumentElement;

并创建新元素

my $new_element= $doc->createElement("library");

然后

$root->insertBefore($new_element,undef);

最后:

my $root = $doc->getDocumentElement;
my $new_element= $doc->createElement("library");
$parent = $root->parentNode;
$root->insertBefore($new_element,$parent);

但它不会工作。还试图找到返回头节点的根的父节点,然后是addchild,但它也不起作用。

【问题讨论】:

  • 更好地展示你真实的完整脚本
  • 我的 $root = $doc->getDocumentElement;我的 $new_element= $doc->createElement("library"); $parent = $root->parentNode; $root->insertBefore($new_element,$parent);

标签: perl parent add libxml2


【解决方案1】:

试试这个:

open my $in, '<:encoding(utf-8)', 'in.xml';
my $document = XML::LibXML->load_xml(IO => $in);

my $root     = $document->documentElement();
my $new_root = $document->createElement('library');
$new_root->appendChild($root);
$document->setDocumentElement($new_root);

open my $out, '>:encoding(utf-8)', 'out.xml';
print $out $document->toString();

它创建 new_root 元素,将 root 元素作为子元素附加到 new_root 并将 new_root 元素设置为根文档元素。

【讨论】:

    【解决方案2】:

    您需要创建一个新的空library 元素并将其设置为文档的新根元素。然后将旧根添加为新子。

    use strict;
    use warnings;
    
    use XML::LibXML;
    
    my $doc = XML::LibXML->load_xml(string => << '__END_XML__');
    <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
    </book>
    __END_XML__
    
    my $book = $doc->documentElement;
    my $library = $doc->createElement('library');
    $doc->setDocumentElement($library);
    $library->appendChild($book);
    
    print $doc->toString(1);
    

    输出

    <?xml version="1.0"?>
    <library>
      <book id="bk104">
        <author>Corets, Eva</author>
        <title>Oberon's Legacy</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2001-03-10</publish_date>
        <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
      </book>
    </library>
    

    【讨论】:

    • 是的,这对我有用,但我没有意识到它不能解决我的问题,因为我正在处理 xml 查询,所以我在添加新根后进行解析,但我需要在解析我的最终结果后添加它数据。假设我需要从 解析 节点必须将 作为根写入输出。我用 find 解析:我的 $_demo = $root->find("//$sel_element");对于此示例,其中 $sel_element 是“book”节点。而且我不能像 $new_root->appendChild($_demo); 这样追加新的根目录因为我得到了 appendchild 错误。获得最终解析数据后,我应该如何添加它?
    • @user2158951:你的问题一点都不清楚。您必须展示一个完整的示例来说明您想要做什么,包括输入及其到达的顺序,以及所需的输出 XML 结构。请为此编辑您的问题。
    • 您的示例不起作用的原因是$root-&gt;find("//book") 返回一个XML::LibXML::NodeList 对象,而$new_root-&gt;appendChild 需要一个XML::LibXML::Node。您必须说明是要追加列表中的第一个节点,还是遍历所有节点并追加每个节点。
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2022-01-23
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多