Olá, não estou conseguindo remover informações de um xml, eu procuro em um .err (que é um arquivo texto) os elementos que desejo excluir no .xml, por exemplo:
//Arquivo .err
//Procurar e excluir
s9_509
// Arquivo .xml
// Procurar e excluir s9_509
// Ocorrencias no xml:
// Ocorrencia 1:
<nt id="s9_501" cat="pp">
<edge label="H" idref="s9_1"/>
<edge label="DP" idref="s9_502"/>
<edge label="STA" idref="s9_509"/>
<edge label="P" idref="s9_19"/>
<edge label="S" idref="s9_510"/>
<edge label="PU" idref="s9_25"/>
</nt>
// Ocorrencia 2:
<nt id="s9_509" cat="fcl">
</nt>
E segue a minha classe onde faço as operações:
public class Manager {
private Document document;
private ArrayList<String> warnings;
public Manager(){
this.warnings = new ArrayList();
}
public void readXML(String source) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException{
File xml = new File(source);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(xml);
document = doc;
System.out.println("Leu o xml com sucesso");
}
public void readERR(String source) throws FileNotFoundException{
warnings.clear(); // Limpa a lista de nodos toda a vez que chama este método
File file = new File(source);
FileReader fr = new FileReader(file);
BufferedReader bf = new BufferedReader(fr);
int count = -1;
try {
while(bf.readLine() != null){
if(bf.readLine().contains("warning")){
warnings.add(this.toInt(bf.readLine().trim()));
System.out.println(warnings.get(++count).trim());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Leu os warnings");
}
private String toInt(String text){
int begin = -1;
int end = -1;
for(int i = 0; i < text.length(); i++){
if(text.charAt(i) == 's' && Character.isDigit(text.charAt(i+1))){
begin = i;
end = i+7;
}
}
if(begin != -1 && end != -1){
return text.substring(begin, end);
}
else
throw new NumberFormatException();
}
public void removeWarnings() throws XPathExpressionException{
XPath xpath = XPathFactory.newInstance().newXPath();
for(int index = 0; index < warnings.size(); index++){
XPathExpression expr = xpath.compile("/s/graph/nonterminal/nt[@id="+warnings.get(index)+"]"); // Caminho para os warnings
Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
nodes.item(i).removeChild(nodes.item(i)); // Remove o nodo selecionado
System.out.println("Removeu nt: "+warnings.get(i));
}
expr = xpath.compile("/s/graph/nonterminal/nt/edge[@idref="+warnings.get(index)+"]"); // Warnings são os erros
result = expr.evaluate(document, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
nodes.item(i).removeChild(nodes.item(i)); // Remove o nodo selecionado
System.out.println("Removeu edge: "+warnings.get(i));
}
}
}
public void saveXML(String path){
//grava o documento XML editado
DOMSource source = new DOMSource(document);
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
StreamResult result = new StreamResult(new FileOutputStream(new File(path)));
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Como eu disse mais acima, como posso excluir os erros que estão no .err do .xml?
O result e o expr do método removeWarnings() não estão nulos, mas o nodes.getLength() do mesmo método é igual a 0.
Alguem sabe como me ajudar?