2
XMLのいくつかの値を変更する必要がありますが、.setText行を実行すると、java.lang.NullPointerExceptionエラーが表示され、その理由を理解できません。xmlのノードのエラー値の変更
<?xml version="1.0" encoding="UTF-8"?>
<prueba>
<reg id="576340">
<dato cant="680" id="1" val="-1" num="" desc="" />
<dato cant="684" id="5" val="-1" num="" desc="" />
<dato cant="1621" id="1" val="-1" num="" desc="Hi" />
<dato cant="1625" id="5" val="-1" num="" desc="Hola" />
</reg>
</prueba>
これはコードです:
public static void main(String[] args) throws FileNotFoundException,
JDOMException, IOException {
File xml = new File("c:\\prueba3.xml");
XMLOutputter xmlOut = new XMLOutputter();
Document doc = (Document) new SAXBuilder().build(xml);
Element raiz = doc.getRootElement();
List articleRow = raiz.getChildren("reg");
for (int i = 0; i < articleRow.size(); i++) {
Element row = (Element) articleRow.get(i);
List images = row.getChildren("dato");
for (int j = 0; j < images.size(); j++) {
Element row2 = (Element) images.get(j);
String texto = row2.getAttributeValue("desc") ;
String id = row2.getAttributeValue("id");
if ((texto != null) && (texto !="") &&
(id.equals("1") || id.equals("2"))){
row2.getChild("desc").setText("valor");
}
}
}
xmlOut.setFormat(Format.getPrettyFormat());
xmlOut.output(doc, new FileWriter("c:\\prueba3.xml"));
System.out.println("fin");
}
ご挨拶と感謝。
する必要がありますが正しい、それはrow2.getAttribute( "DESC")のsetValue( "勇気")である必要があります。 – Waqas
row2.getAttribute( "desc")を指定すると、setValue( "valor"); ご迷惑をおかけして申し訳ございません。 挨拶。 –