亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 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 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號