我想更改java中XML标记的值
以下是我的XML:我想更改java中XML标记的值,java,xml,Java,Xml,以下是我的XML: <Connections> <Connection> <userName>infadf</userName> <password>tcslkvcvo@123</password> <schemaName>dbo</schemaName> <status>OLD</status> <portNum>600
<Connections>
<Connection>
<userName>infadf</userName>
<password>tcslkvcvo@123</password>
<schemaName>dbo</schemaName>
<status>OLD</status>
<portNum>600687</portNum>
<aliasName>first</aliasName>
</Connection>
</Connections>
英法德
tcslkvcvo@123
dbo
古老的
600687
第一
我想将标记“status”的值更改为“NEW”
下面是我的java代码:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
try
{
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(new File(path).exists())
{
Document doc = null;
try {
doc = dBuilder.parse(path);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Connection");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
Element eElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
}
if(eElement.getElementsByTagName("status").item(0).getTextContent().equalsIgnoreCase("OLD"))
{
eElement.getElementsByTagName("status").item(0).setTextContent("NEW");
}
}
}
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=null;
尝试
{
dBuilder=dbFactory.newDocumentBuilder();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
if(新文件(路径).exists())
{
单据单据=空;
试一试{
doc=dBuilder.parse(路径);
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“连接”);
对于(int-temp=0;temp
XML中状态标记的值未更改。
请帮忙。试试这个
if(“status”.equalsIgnoreCase((nNode.getNodeName())){nNode.setTextContent(“2000000”);}
您编写的代码正在工作
可能是你检查它是否正常工作的方式是错误的。
我已经检查了您所做的XML更新
你对你所做的还有什么怀疑吗?由于我的声誉评分,我无法发布图像,否则我会显示从eclipse获得的输出。在您更改后-是否将其保存回文件?您正在链接其内存值,您需要将值写入文件以实现所需。是的,我已在写入后再次保存XML文件,但tag的值也未更改请检查