如何使用正则表达式从XML中提取标签名称

问题描述 投票:-2回答:1

我有一个要解决的问题。我有2种格式的xml请求

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="urn:x-facebook-com:DEF.plan.services.test">
  <OneRequest>
    <page_number>1</page_number>
    <page_size>25</page_size>
    <origin>TEST</origin>
    <item_name/>
  </OneRequest>
</Request>

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="urn:x-google-com:ABC.plan.services.plans">
 <SecondRequest/>
</Request>

[在两种情况下,我都希望提取作为<Request>之后的第一个标签名称。即OneRequestSecondRequest(它们将是动态的,并且有100个)。我尝试使用正则表达式,但没有得到我想要的。任何意见或建议将不胜感激。

也确实看到了有关xml解析器的帖子,但对于我基本上想要的只是[<Request>之后的第一个标签]似乎有些过头了>

我的尝试

String[] requestTags = requestBody.split("</");
String requestName = requestTags[requestTags.length-2].replaceAll("[^a-zA-Z0-9]",

不是最好的方法,但是在第二种方法中完全弄乱了

我有一个要解决的问题。我有2种格式的xml请求[[[[

java regex
1个回答
0
投票
您基本上只需要正则表达式的\s选项即可实现:
© www.soinside.com 2019 - 2024. All rights reserved.