我正在尝试阅读XML
我的XML文件是
<datasource caption='Benfords' inline='true' name='oracle.41859.392947812499' version='8.2'>
<connection authentication='RDBMS' class='oracle' port='1531' **server**='honeyWillServer' service ="de06" username='IIUSER'>
<relation name='TableauSQL' type='text'>
</Connection>
</datasource>
public class XMLReader {
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("C:\\Users\\c200433\\Desktop\\RBM - Law.twb"); // XML file
Document doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
Element staff = rootNode.getChild("datasource");
Element staff1 = rootNode.getChild("connection");
staff1.getAttribute("server").setValue("aventador.am.lilly.com:1530/tst806");
System.out.println("File updated!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
任务很简单我想钻取xml并使用setValue将服务器(在连接节点中)替换为所需的。
我收到了java.lang.NullPointerException。
答案 0 :(得分:1)
首先,
您的XML格式不正确:
<connection> </Connection> --? capital 'C' in end tag
<relation name='TableauSQL' type='text'>
- 关系的结束标记在哪里。?
修好后,尝试以下,
Element staff1 = staff.getChild("connection"); // Connection is the child of datasource.
而不是:
Element staff1 = rootNode.getChild("connection");