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

  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>


九州编程
浏览 77回答 2
2回答

慕斯709654

public void loadSettings(String variableName) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File inputFile = new File("data.xml");&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; Document doc = dBuilder.parse(inputFile);&nbsp; &nbsp; &nbsp; &nbsp; doc.getDocumentElement().normalize();&nbsp; &nbsp; &nbsp; &nbsp; NodeList nList = doc.getElementsByTagName("Setting");&nbsp; &nbsp; &nbsp; &nbsp; for (int temp = 0; temp < nList.getLength(); temp++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node nNode = nList.item(temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nNode.getNodeType() == Node.ELEMENT_NODE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element eElement = (Element) nList.item(temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList VariableName = eElement.getElementsByTagName("VariableName");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList VariableValue = eElement.getElementsByTagName("VariableValue");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (VariableName.item(0).getTextContent().equalsIgnoreCase(variableName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String txtBookmarkUrlValue = VariableValue.item(0).getLastChild().getTextContent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(txtBookmarkUrlValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txtBookmarkUrl.setText(txtBookmarkUrlValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}这行得通,但是如果您有更可靠的答案,您可以分享。

慕桂英3389331

首先创建一个代表您的设置的对象。案例是在整个应用程序中重用它的值。我假设您一开始只会使用一次,并且设置不会改变。单例模式适合那里。final class Settings{&nbsp; &nbsp; private static volatile Settings instance = null;&nbsp; &nbsp; private boolean _isDeathByCaptcha;&nbsp; &nbsp; private boolean _manualCaptcha;&nbsp; &nbsp; ...&nbsp; &nbsp; //getters & setters&nbsp; &nbsp; public boolean isDeathByCaptcha(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return _isDeathByCaptcha;&nbsp; &nbsp; }&nbsp; &nbsp; public void setIsDeathByCaptcha(boolean isDeathByCaptcha){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this._isDeathByCaptcha = isDeathByCaptcha;&nbsp; &nbsp; }&nbsp; &nbsp; private Settings(){}&nbsp; &nbsp; public static Settings getInstance(){&nbsp; &nbsp; &nbsp; &nbsp; if(instance == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (Settings.class) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (instance == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instance = new Settings();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return instance;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}之后,您可以致电Settings.getInstance().isDeathByCaptcha();获取您的价值。当然你需要用 setter 更早地设置它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java