【问题标题】:How can I search a XML file using regular expressions in php如何在 php 中使用正则表达式搜索 XML 文件
【发布时间】:2018-06-28 00:28:18
【问题描述】:

我很好奇是否可以使用正则表达式在 XML 文件中搜索某个标签。如果我使用 fopen('foo.xml'); 我可以搜索文件但它只允许我搜索标签之间的内容,而不是它们自己的标签。我的目标是希望创建一个函数,允许我删除两个标签之间的所有内容,例如在 xml 文件中的用户之间。我使用的语言是 PHP。

提前谢谢约翰。

【问题讨论】:

  • 我不推荐使用正则表达式。请参阅stackoverflow.com/questions/3577641/… 以获得更好的选择
  • 请发布您尝试过的示例数据和示例代码。至于正则表达式,Ungreedy 修饰符 U 将帮助您。
  • 好的,将尝试使用 SimpleXML 库或 DOM,谢谢 phil。
  • 我们确实需要看到您的 xml 的最小但准确的表示;您的编码尝试;以及您的预期结果。

标签: php xml


【解决方案1】:

您应该使用 SimpleXML 之类的东西来处理/编辑 XML 文件。

如果您真的坚持通过将 SML 文件视为 string 来执行此操作,您可以执行类似的操作(或者您可以使用正则表达式)。但是您应该使用 XML 库。

// get your file as a string 
$yourXML = file_get_contents($file) ;

$posStart = stripos($yourXML,'<users>') + strlen('<users>') ;
$posEnd = stripos($yourXML,'</users>')  ;

$newXML = substr($yourXML,0,$posStart) . substr($yourXML,$posEnd) ;

// <users> is now empty
echo $newXML ;

【讨论】:

  • 谢谢,我会试试 SimpleXML 库,谢谢 Mr.Glass。
【解决方案2】:

DomDocument 和 XPath 将使事情变得非常干净、直接和可靠。

您可以使用evaluate()query(),因为它们提供相同的结果。

// 将寻找匹配的标签,无论其位置如何。

请注意,我的解决方案区分大小写。

代码:(Demo)

$xml = <<<XML
<myXml>
<Person>
  <firstName>pradeep</firstName>
  <lastName>jain</lastName>
  <address>
    <doorNumber>287</doorNumber>
    <street>2nd block</street>
    <city>bangalore</city>
  </address>
  <phoneNums type="mobile">9980572765</phoneNums>
  <phoneNums type="landline">080 42056434</phoneNums>
  <phoneNums type="skype">123456</phoneNums>
</Person>
<Person>
  <firstName>pradeep</firstName>
  <lastName>jain</lastName>
  <address>
    <doorNumber>287</doorNumber>
    <street>2nd block</street>
    <city>bangalore</city>
  </address>
  <phoneNums type="mobile">1</phoneNums>
  <phoneNums type="landline">2</phoneNums>
  <phoneNums type="skype">3</phoneNums>
</Person>
</myXml>
XML;

$dom = new DOMDocument; 
$dom->loadXML($xml);  // <-- you'll need to import your file instead of a string as demo'ed here
$xpath = new DOMXPath($dom);
echo count($xpath->evaluate("//phoneNums")) , "\n";  // 6
echo count($xpath->evaluate("//street")) , "\n";  // 2
echo count($xpath->evaluate("//myXml")) , "\n";  // 1
echo count($xpath->evaluate("//Person")) , "\n";  // 2
echo count($xpath->evaluate("//person")) , "\n";  // 0 <-- case-sensitive

【讨论】:

  • 感谢 mickmackusa 我目前正在研究 xpath。
  • 请用更多细节改进您的问题,这样您就不会一直受到反对票的打击。 ...然后继续研究。
【解决方案3】:

作为在 SimpleXML 中执行此操作所需的各个部分的简单模型,您需要了解一些概念才能使其工作。

主要的是 XPath,它是一种用于 XML 的 SQL。当然,它有自己的符号,有时可能有点迂腐,但你可以在 https://codebeautify.org/Xpath-Tester 这样的网站上进行试验。

$data = '<?xml version="1.0" encoding="UTF-8"?>
<Users>
    <User id="123">
        <Name>fred</Name>
        <Extension>1234</Extension>
    </User>
    <User id="124">
        <Name>bert</Name>
        <Extension>1235</Extension>
    </User>
    <User id="125">
        <Name>foo</Name>
        <Extension>1236</Extension>
    </User>
</Users>';

$userID = "123";

$users = simplexml_load_string($data);

// Find the user with the id attribute (use [0] as the call to xpath 
// returns a list of matches and you only want the first one)
$userMatch = $users->xpath("//User[@id='{$userID}']")[0];

// Just output user id attribute and name
echo "id=".$userMatch['id'].",name=".$userMatch->Name.PHP_EOL;
echo "Removing user...".PHP_EOL;

// Remove the user - note the [0] is required here
unset($userMatch[0]);

// Print out the resulting XML after the removal
echo $users->asXML();

我已经通过代码介绍了 cmets 的工作原理。输出是...

id=123,name=fred
Removing user...
<?xml version="1.0" encoding="UTF-8"?>
<Users>

    <User id="124">
        <Name>bert</Name>
        <Extension>1235</Extension>
    </User>
    <User id="125">
        <Name>foo</Name>
        <Extension>1236</Extension>
    </User>
</Users>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多