удалить все вхождения определенного атрибута из XML - PullRequest
0 голосов
/ 09 октября 2019

У меня есть 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>

Я хочу удалить все вхождения атрибута «ExposureSentence». Вывод будет

  <word Endpoint="1" SciomeSRIE_Sentence.ExposureSentence="1">gutkha</word>
    <word >split_identifier ,</word>
    <word >and</word>
    <word >what</word>
    <word >role</word>
    <word >split_identifier ,</word>
    <word >if</word>
    <word >any</word>
    <word >split_identifier ,</word>
    <word >nicotine</word>
    <word >contributes</word>
    <word >to</word>
    <word >the</word>
    <word >effects</word>
    <word >split_identifier .</word>
    <word EB_NLP_Tagger.Participant="3" AnimalGroupSentence="1" DoseGroupSentence="1" >Adult</word>
    <word EB_NLP_Tagger.Participant="3" Sex="1" AnimalGroupSentence="1" DoseGroupSentence="1" >male</word>
    <word EB_NLP_Tagger.Participant="3" Species="1" AnimalGroupSentence="1" DoseGroupSentence="1" >mice</word>
    <word AnimalGroupSentence="1" DoseGroupSentence="1" >were</word>
    <word AnimalGroupSentence="1" DoseGroupSentence="1" >treated</word>
    <word AnimalGroupSentence="1" DoseGroupSentence="1" >daily</word>
    <word AnimalGroupSentence="1" DoseGroupSentence="1" >for</word>

Я пытался следовать, но не уверен, как действовать дальше.

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
        NodeList sectionNodeList = doc.getElementsByTagName("section");
        for (int i = 0; i < sectionNodeList.getLength(); i++)
        {
            Node sectionNode = sectionNodeList.item(i);

        }

1 Ответ

0 голосов
/ 09 октября 2019

Я думаю, что самым простым решением будет заменить все вхождения ExposureSentence="1" с помощью простого регулярного выражения. Прочитайте все содержимое xml как String и замените все определенные вхождения слова, где вам не требуется синтаксический анализ и замена XML.

В случае синтаксического анализа XML у вас есть анализ, манипулирование логикой и вам необходимо перестроить инфо-набор XML.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...