Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从两个不同内容之间的XML文件中获取数据_Xml_Linux_Bash_Shell_Sed_Perl - Fatal编程技术网

从两个不同内容之间的XML文件中获取数据

从两个不同内容之间的XML文件中获取数据,xml,linux,bash,shell,sed,perl,Xml,Linux,Bash,Shell,Sed,Perl,我试图在两个XML标记之间获取数据,如 <page size="10" seconds="184" name="TEST_ONE" query="test environment"> <content1>...</content1> <content2>...</content2> </page> ... ... 我试过了 cat ABC.XML | grep -oP '(?<=<page size

我试图在两个XML标记之间获取数据,如

<page size="10" seconds="184" name="TEST_ONE" query="test environment">
  <content1>...</content1>
  <content2>...</content2>
</page>

...
...
我试过了

cat ABC.XML | grep -oP '(?<=<page size="10" seconds="184" name="TEST_ONE" query="test environment">).*?(?=</page>)'

cat ABC.XML | grep-oP'(?我建议使用
XML
解析器。这里有一个示例及其
XML::Twig
模块:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

XML::Twig->new(
    twig_roots => {
        'page[@query = "test environment"]/*' => sub { $_->print },
    },  
    pretty_print => 'indented',
)->parsefile( shift );
像这样运行:

perl script.pl xmlfile
这将产生:

<content1>...</content1>
<content2>...</content2>
。。。
...

xmlstarlet
营救

xmlstarlet sel -t -v 'page' ABC.XML
对于所有类型的其他格式化和查询都有选项

如果需要子元素,请使用

xmlstarlet sel -t -c 'page/*' ABC.XML
并在对其他答案的评论中给出您的“查询”答案:

xmlstarlet sel -t -c "page[@query='test environment']/*" ABC.XML
又快又脏:

sed -e'1,/<page.*query="test environment">/ d' -e'/<\/page>/,$ d' abc1.xml 
sed-e'1,//d'-e'//,$d'abc1.xml

我还有一个问题,我在同一个XML中有多个页面标记。因此我想得到一个包含名为query=“test environment”的查询的页面。是否会有任何空间问题?@San:编辑以添加属性条件
[@query=“test environment”]
。此外,我必须正确地给出include“XML FILE”命令。希望这能奏效。
sed -e'1,/<page.*query="test environment">/ d' -e'/<\/page>/,$ d' abc1.xml