package com.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Xml {
public static void main(String[] args) {
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/com/test/a.xml");
XPathFactory pathFactory = XPathFactory.newInstance();
XPath xpath = pathFactory.newXPath();
XPathExpression pathExpression = xpath
.compile("//@href");
Node nodeUpdate = null;
Object result = pathExpression.evaluate(doc, XPathConstants.NODESET);
String strId ="";
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
String str= nodes.item(i).getNodeValue();
strId = str.substring(str.indexOf("#")+1);
System.out.println(strId);
nodeUpdate = nodes.item(i);
nodeUpdate.setNodeValue("bbbb");
}
// String newStr = "src/com/test/a.xml";
//
//
// XmlWriter writer = new XMLWriter(new FileWriter(new File(newStr)));
//
// writer.write(doc);
//
// writer.close();
//
//
// Document document = builder.parse("src/com/test/b.xml");
// String str= "//*[@id='"+strId+"']";
// XPathExpression pathExpression_b = xpath
// .compile(str);
//
//
// Object result_b = pathExpression_b.evaluate(document, XPathConstants.NODESET);
// NodeList nodes_b = (NodeList) result_b;
// for (int i = 0; i < nodes_b.getLength(); i++) {
// Node objNode = nodes_b.item(i);
//
// String str1= nodes_b.item(i).getNodeName();
// String str2 = nodes_b.item(i).getTextContent();
//
//
// }
//
}catch(Exception e){
e.printStackTrace();
}
}
}