在 Spark Scala 中过滤掉命名空间 xml

Filter out namespaces xml in Spark Scala

我正在尝试从 spark 流读取的 xml 中过滤名称空间信息。下面是一个示例 xml。这是我正在尝试的代码。它应该从 xml 中过滤掉所有 "ns:0"、"ns:1"...。由于火花流,xml 将作为 rdd 读入。

val message_filter = message.filter(x => x.matches("([n][s][0-9]:)+")))

<?xml version="1.0"?>
<Period>
  <AllContacts>
    <Entry>
      <ns0:entity-Person>
        <ns0:CellPhone>3095550101</ns0:CellPhone>
        <ns0:FirstName>Brrzzz</ns0:FirstName>
        <ns0:LastName>Grbbs</ns0:LastName>
      </ns0:entity-Person>
      <ns0:PrimaryPhone>mobile</ns0:PrimaryPhone>
    </Entry>
  </AllContacts>
  <State>TX</State>
</Period>

所需格式:

<?xml version="1.0"?>
<Period>
  <AllContacts>
    <Entry>
      <entity-Person>
        <CellPhone>3095550101</CellPhone>
        <FirstName>Brrzzz</FirstName>
        <LastName>Grbbs</LastName>
      </entity-Person>
      <PrimaryPhone>mobile</PrimaryPhone>
    </Entry>
  </AllContacts>
  <State>TX</State>
</Period>

如果 x.matches 接受正则表达式,那么您的正则表达式应该如下所示:/ns\d+:([\w-]+)/g 这是 regex101.com

中的示例