首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用xpath更新xml

使用xpath更新xml
EN

Stack Overflow用户
提问于 2015-11-30 01:56:54
回答 2查看 67关注 0票数 2

我只想更改jojo餐厅的评级,我尝试了这个代码,但没有工作,请帮助。下面是我的xml文档和代码。

代码语言:javascript
运行
复制
<city> 
    <beirut> 
        <restaurant> 
            <name>sada</name> 
        </restaurant> 
    </beirut>  
    <jbeil> 
        <restaurant> 
            <name>toto</name>  
            <rating>4.3/5</rating> 
        </restaurant>  
        <restaurant> 
            <name>jojo</name>  
            <rating>4.3/5</rating> 
        </restaurant> 
    </jbeil>  
    <sour> 
        <restaurant> 
            <name>sada</name> 
        </restaurant> 
    </sour> 
</city>

代码:

代码语言:javascript
运行
复制
  try {
     File inputFile = new File("src/xpath/josephXml.xml");
     DocumentBuilderFactory dbFactory 
        = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder;

     dBuilder = dbFactory.newDocumentBuilder();

     Document doc = dBuilder.parse(inputFile);
     doc.getDocumentElement().normalize();

     XPath xPath =  XPathFactory.newInstance().newXPath();

     String expression = "/City/Jbeil/Restaurants/Restaurant[name='Feniqia']/rating"; 


     Element e = (Element)xPath.evaluate(expression, doc, XPathConstants.NODE);

     if (e != null){
        e.setTextContent("5/5");
     }

  } catch (ParserConfigurationException | SAXException | IOException e) {
  }
EN

Stack Overflow用户

发布于 2015-11-30 06:16:15

首先,您的XPath没有与发布的XML对齐(没有<restaurants>Feniqia名称),而且所有内容都应该是小写的。您可能从大型xml文档中提取了一部分。为了进行演示,我使用了jojo餐厅。

其次,XPath本身不会更新XML,您需要创建一个新文档。只需在结尾处添加一个转换:

代码语言:javascript
运行
复制
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.xpath.*;

import java.io.File;
import java.io.IOException;

import org.xml.sax.*;
import org.w3c.dom.*;

public class RatingUpdate {    

    public static void main(String [] args) {

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

               DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
               DocumentBuilder dBuilder;

               dBuilder = dbFactory.newDocumentBuilder();

               Document doc = dBuilder.parse(inputFile);
               doc.getDocumentElement().normalize();

               XPath xPath =  XPathFactory.newInstance().newXPath();

               String expression = "/city/jbeil/restaurant[name='jojo']/rating"; 

               Element e = (Element)xPath.evaluate(expression, doc, XPathConstants.NODE);

               if (e != null){
                  e.setTextContent("5/5");
               }

               Transformer xformer = TransformerFactory.newInstance().newTransformer();
               xformer.transform(new DOMSource(doc), new StreamResult(new File("src/xpath/josephXml_update.xml")));

             } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException |
                      TransformerException e) {

          }
    }
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33986034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档