Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java dom解析器getElement_Java_Xml_Parsing_Dom - Fatal编程技术网

Java dom解析器getElement

Java dom解析器getElement,java,xml,parsing,dom,Java,Xml,Parsing,Dom,我只想在贝鲁特的餐馆吃饭,请帮忙 这是我的xml文件的一部分: 城市->贝鲁特->餐厅->餐厅->名称-> tyr->restaurants->restaurant->name-> jbeil->restaurants->restaurant->name-> ... ... ... 因为这个代码给了我所有城市的所有餐厅: 试试{ File inputFile = new File("src/jos

我只想在贝鲁特的餐馆吃饭,请帮忙

这是我的xml文件的一部分:

城市->贝鲁特->餐厅->餐厅->名称->

    tyr->restaurants->restaurant->name-> 

    jbeil->restaurants->restaurant->name->


...


...


...

因为这个代码给了我所有城市的所有餐厅:

试试{

 File inputFile = new File("src/josephXml.xml");

     DocumentBuilderFactory dbFactory 

        = DocumentBuilderFactory.newInstance();

     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

     Document doc = dBuilder.parse(inputFile);

     doc.getDocumentElement().normalize();






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

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

    Node nNode = nList.item(temp);


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

           Element eElement = (Element) nNode; 



            jTextArea1.append( "\n"+"Name      :   "+ eElement
              .getElementsByTagName("name")
              .item(0)
              .getTextContent()+"\n "
File inputFile=new文件(“src/josephXml.xml”);
DocumentBuilderFactory数据库工厂
=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“餐厅”);
对于(int-temp=0;temp

,我只想要贝鲁特的餐馆,请帮助您使用餐馆,但您的文件使用餐馆。这不好,因为区分大小写

使用XPath:您可以选择很多东西,它非常可读

在代码中替换此选项:

XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/city/beirut/restaurants/restaurant";

NodeList nList = (NodeList)  xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
<restaurants>

  <restaurant>

     <name>

     ...

 </restaurant>

</restaurants>
 File inputFile = new File("src/josephXml.xml");

     DocumentBuilderFactory dbFactory 

        = DocumentBuilderFactory.newInstance();

     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

     Document doc = dBuilder.parse(inputFile);

     doc.getDocumentElement().normalize();






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

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

    Node nNode = nList.item(temp);


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

           Element eElement = (Element) nNode; 



            jTextArea1.append( "\n"+"Name      :   "+ eElement
              .getElementsByTagName("name")
              .item(0)
              .getTextContent()+"\n "
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/city/beirut/restaurants/restaurant";

NodeList nList = (NodeList)  xPath.compile(expression).evaluate(document, XPathConstants.NODESET);