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

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

使用第一行文本值java獲取第二行xml

使用第一行文本值java獲取第二行xml

九州編程 2022-07-20 20:24:06
  public void loadSettings() {        try {            File inputFile = new File("data.xml");            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();            Document doc = dBuilder.parse(inputFile);            doc.getDocumentElement().normalize();            NodeList nList = doc.getElementsByTagName("Setting");            for (int temp = 0; temp < nList.getLength(); temp++) {                Node nNode = nList.item(temp);                if (nNode.getNodeType() == Node.ELEMENT_NODE) {                    Element eElement = (Element) nList.item(temp);                    NodeList  VariableName = eElement.getElementsByTagName("VariableName");                    NodeList  VariableValue = eElement.getElementsByTagName("VariableValue");                    System.out.println(VariableName.item(0).getTextContent());                    if (VariableName.item(0).hasChildNodes()) {                    }//                    txtBookmarkUrl.setText(bookMarkUrl);                }            }        } catch (Exception e) {            e.printStackTrace();        }    }我想創建一個函數,在設置元素中獲取 xml 的第二部分。我希望該函數返回一個結果,以便在 swing GUI 啟動時將其分配給文本字段默認值。該函數應該使用“isDecaptcher”變量名并返回“0”變量值。<Bookmark>  <Setting>    <VariableName>isDeathbycaptcha</VariableName>    <VariableValue>0</VariableValue>  </Setting>  <Setting>    <VariableName>isDecaptcher</VariableName>    <VariableValue>0</VariableValue>  </Setting>  <Setting>    <VariableName>isExpertdecoders</VariableName>    <VariableValue>0</VariableValue>  </Setting>  <Setting>    <VariableName>ManualCaptcha</VariableName>    <VariableValue>1</VariableValue>  </Setting></Bookmark>
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

public void loadSettings(String variableName) {


    try {

        File inputFile = new File("data.xml");

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

        Document doc = dBuilder.parse(inputFile);

        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("Setting");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nList.item(temp);

                NodeList VariableName = eElement.getElementsByTagName("VariableName");

                NodeList VariableValue = eElement.getElementsByTagName("VariableValue");

                if (VariableName.item(0).getTextContent().equalsIgnoreCase(variableName)) {


                    String txtBookmarkUrlValue = VariableValue.item(0).getLastChild().getTextContent();

                    System.out.println(txtBookmarkUrlValue);

                    txtBookmarkUrl.setText(txtBookmarkUrlValue);

                }

            }

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

這行得通,但是如果您有更可靠的答案,您可以分享。


查看完整回答
反對 回復 2022-07-20
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

首先創建一個代表您的設置的對象。案例是在整個應用程序中重用它的值。我假設您一開始只會使用一次,并且設置不會改變。單例模式適合那里。


final class Settings{


    private static volatile Settings instance = null;


    private boolean _isDeathByCaptcha;

    private boolean _manualCaptcha;


    ...

    //getters & setters

    public boolean isDeathByCaptcha(){

         return _isDeathByCaptcha;

    }


    public void setIsDeathByCaptcha(boolean isDeathByCaptcha){

         this._isDeathByCaptcha = isDeathByCaptcha;

    }


    private Settings(){}


    public static Settings getInstance(){

        if(instance == null){

            synchronized (Settings.class) {

                if (instance == null) {

                    instance = new Settings();

                }

            }

        }

        return instance;        

    }

}

之后,您可以致電Settings.getInstance().isDeathByCaptcha();獲取您的價值。當然你需要用 setter 更早地設置它。


查看完整回答
反對 回復 2022-07-20
  • 2 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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