为了账号安全,请及时绑定邮箱和手机立即绑定

从 XML 中删除所有出现的特定属性

从 XML 中删除所有出现的特定属性

慕田峪7331174 2023-10-12 14:38:06
我有一个 XML 文件,内容如下<document>  <section>    <section SectionName="abstract">     <paragraph>    <word Endpoint="1" SciomeSRIE_Sentence.ExposureSentence="1">gutkha</word>    <word ExposureSentence="1">split_identifier ,</word>    <word ExposureSentence="1">and</word>    <word ExposureSentence="1">what</word>    <word ExposureSentence="1">role</word>    <word ExposureSentence="1">split_identifier ,</word>    <word ExposureSentence="1">if</word>    <word ExposureSentence="1">any</word>    <word ExposureSentence="1">split_identifier ,</word>    <word ExposureSentence="1">nicotine</word>    <word ExposureSentence="1">contributes</word>    <word ExposureSentence="1">to</word>    <word ExposureSentence="1">the</word>    <word ExposureSentence="1">effects</word>    <word ExposureSentence="1">split_identifier .</word>    <word EB_NLP_Tagger.Participant="3" AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">Adult</word>    <word EB_NLP_Tagger.Participant="3" Sex="1" AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">male</word>    <word EB_NLP_Tagger.Participant="3" Species="1" AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">mice</word>    <word AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">were</word>    <word AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">treated</word>    <word AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">daily</word>    <word AnimalGroupSentence="1" DoseGroupSentence="1" ExposureSentence="2">for</word>
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

XPath 使这变得简单:


public static void main(String... args)

        throws Exception

{

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

    Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));


    XPathFactory xPathfactory = XPathFactory.newInstance();

    XPath xpath = xPathfactory.newXPath();


    // Find word elements with ExposureSentence attribute

    XPathExpression query = xpath.compile("//word[@ExposureSentence]");

    NodeList words = (NodeList) query.evaluate(doc, XPathConstants.NODESET);

    for (int i = 0; i < words.getLength(); i++) {

        // Remove the attribute

        ((Element) words.item(i)).removeAttribute("ExposureSentence");

    }


    // Handle ComponentName

    query = xpath.compile("//ComponentName");

    NodeList componentNames = (NodeList) query.evaluate(doc, XPathConstants.NODESET);

    for (int i = 0; i < componentNames.getLength(); i++) {

        String content = componentNames.item(i).getTextContent();

        componentNames.item(i).setTextContent(

            Arrays.stream(content.split(","))

                .map(String::trim)

                .filter(s -> !s.equals("ExposureSentence"))

                .collect(Collectors.joining(", ")));

    }


    // Omitted: Save the XML

}


查看完整回答
反对 回复 2023-10-12
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

我认为最简单的解决方案是ExposureSentence="1"使用简单的正则表达式替换所有出现的情况。将所有 xml 内容读取为 String,并替换所有不需要 XML 解析和替换的特定单词出现位置。

在 XML 解析的情况下,您需要解析、操作逻辑,并且必须重建 XML 信息集。


查看完整回答
反对 回复 2023-10-12
  • 2 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信