根据Java

时间:2018-10-26 15:29:01

标签: java xml netbeans

我是Java编程的新手。我有一个任务,我必须获取特定属性的元素。我正在为项目使用netbeans。该属性取自将在xml文件中搜索的用户,并且将显示该属性的元素。我已经搜索了很多,但找不到任何解决方案。请帮帮我。我正在使用XPathFactory。例如,如果我在输入中输入id,则它应显示元素即gemstone。代码不会显示任何错误,但可以正确编译但不会显示任何输出。以下是java和我的xml文件的代码。

java文件

 String data;
       Scanner input = new Scanner (System.in);
       System.out.println("Enter Attribute whose Element you want to be displayed ");
       data = input.nextLine();
       System.out.println(" \n ");
       try {
    File fXmlFile = new File("stonesorg.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        NodeList node;
        node = (NodeList) xpath.evaluate(data, doc, XPathConstants.NODESET);
        for (int x = 0; x < node.getLength(); x++)
        {
            System.out.print(node.item(x).getNodeName()+" ");
        }

         System.out.println(" \n "); 
       }
       catch(ParserConfigurationException | SAXException | IOException | DOMException e)
       {

       }   
    }

xml文件

<gemstone id = "G1">
        <Name name= "Amethyst"> 
        <namehistory> The name comes from the Koine Greek. </namehistory>
        <namemeaning> Intoxicate </namemeaning>
        </Name>
        <identification> 
        <color> Violet  </color>
        <composition> Quartz,Silica (silicon dioxide, SiO2)</composition>
        <mohsScale> 7–lower in impure varieties</mohsScale>
        <crystalSystem> Trigonal</crystalSystem>
        <category> Silicate mineral </category>
        </identification>
        <history>It is from Magaliesburg, South Africa.The Greeks believed amethyst gems could prevent intoxication, while medieval European soldiers wore amethyst amulets as protection in battle in the belief that amethysts heal people and keep them cool-headed.</history>
        <geographicaldistribution>
            <country>Brazil</country>
            <country>Russia</country>
            <country>South Korea</country>
            <country>Colorado</country>
            <country>Texas</country>
            <country>Pennsylvania</country>
            <country>Ontario</country>
        </geographicaldistribution>
        <birthstone>  Birthstone for February   </birthstone>
        <properties>
            <meltingpoint> 
                            <celsius> 1650±75 °C </celsius>
                            <fahrenheit>3002 ±167°F </fahrenheit>
            </meltingpoint>
            <solubility> Insoluble in common solvents </solubility>
            <gravity> 2.65 constant </gravity>
        </properties>
    </gemstone>

1 个答案:

答案 0 :(得分:0)

首先:永远不要抑制异常(如果您不缩减代码示例)。至少放 e.printStackTrace();在那里

catch(ParserConfigurationException | SAXException | IOException | DOMException e)
   {
     e.printStackTrace();
   }   

第二:您的代码不得编译-catch-XPathException

中缺少一个异常

第三(也是主要的):添加XPathException后,您的代码即可正常工作,但是您需要知道输入的XPath是什么。 (请阅读有关XPath的文档和教程)。

这并不像您要获取的Node名称那么简单。只要从顶部文档运行它-是的,您就可以输入gemstone-因为它是根元素,但是对于其他节点,您必须提供有效的XPath-否则您将一无所获。

顺便说一句,尝试使您的打印语句对调试更有益,例如:

System.out.println(node.item(x).getNodeName()+" " + node.item(x).getTextContent());

以后您可以变得更好...

因此,请尝试一些示例:

  • gemstone
  • gemstone/Name/@name
  • gemstone/geographicaldistribution/*
  • gemstone/geographicaldistribution/country
  • //country[3]
  • //country[text()='Texas']