Scala从内部子元素中删除xml元素

时间:2019-06-29 03:11:04

标签: xml scala nodes

我试图使用Scala xml从xml中删除子元素。我花了很多时间弄清楚如何删除子元素,但没有任何效果。请帮助

输入XML:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Header>
        <Header xmlns="http://abcde.com/reportserv">
            <RequestId>kfgndfgldfknbdfbd</RequestId>
            <ResponseTime>13.98</ResponseTime>
        </Header>
    </soap:Header>
    <soap:Body>
        <DetailResponse xmlns="http://abcde.com/reportserv">
            <DetailResult>
                <Detail>
                    <Domain>Not Available</Domain>
                    <Name />
                    <Email />
                    <PostalCode />
                </Detail>
                <Detail>
                    <Domain>Available</Domain>
                    <Name />
                    <Email />
                    <Phone>143-234-1234</Phone>
                    <PostalCode />
                </Detail>
                <Detail>
                    <Domain>Not Available</Domain>
                    <Name />
                    <Email>tp1@gmail.com</Email>
                    <Phone />
                    <PostalCode />
                </Detail>
            </DetailResult>
        </DetailResponse>
    </soap:Body>
</soap:Envelope>

从上面的xml中,我想删除电子邮件和电话元素

输出Xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Header>
        <Header xmlns="http://abcde.com/reportserv">
            <RequestId>kfgndfgldfknbdfbd</RequestId>
            <ResponseTime>13.98</ResponseTime>
        </Header>
    </soap:Header>
    <soap:Body>
        <DetailResponse xmlns="http://abcde.com/reportserv">
            <DetailResult>
                <Detail>
                    <Domain>Not Available</Domain>
                    <Name />
                    <PostalCode />
                </Detail>
                <Detail>
                    <Domain>Available</Domain>
                    <Name />
                    <PostalCode />
                </Detail>
                <Detail>
                    <Domain>Not Available</Domain>
                    <Name />
                    <PostalCode />
                </Detail>
            </DetailResult>
        </DetailResponse>
    </soap:Body>
</soap:Envelope>

我尝试过的代码看起来只有一个级别

def deleteNodes(n: Elem, f: (Node) => Boolean) = {
  n.child.foldLeft(NodeSeq.Empty)((acc, elem) => if (f(elem)) acc else acc ++ elem)
}


var deleted_elements = person.copy(child = deleteNodes(person, (elem) => (elem.label=="Email"|| elem.label=="Phone")))

0 个答案:

没有答案