我想循环遍历每个XML打印的所有元素.我的问题是我在staff1
标签后不断获得空指针异常,即john 465456433 gmail1 area1 city1
这是我的Java代码,用于打印xml文件中的所有元素:
File fXmlFile = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("*");
System.out.println("----------------------------");
Node n=null;
Element eElement=null;
for (int i = 0; i < nList.getLength(); i++) {
System.out.println(nList.getLength());
n= nList.item(i);
System.out.println("\nCurrent Element :" + n.getNodeName());
if (n.getNodeType() == Node.ELEMENT_NODE) {
eElement = (Element) n.getChildNodes();
System.out.println("\nCurrent Element :" + n.getNodeName());
name = eElement.getElementsByTagName("name").item(i).getTextContent(); //here throws null pointer …
Run Code Online (Sandbox Code Playgroud) 我有一个servlet,传递给查询参数,从DAO获取对象列表,将列表转换为JSON,并将其发送回响应中.每个列表都由具有方法的对象组成:
public String getAsJson(){...}
Run Code Online (Sandbox Code Playgroud)
servlet有一堆看似简单的方法:
private String getUserListAsJson() {
List<User> userList = this.dao.getUsers();
StringBuilder builder = new StringBuilder();
builder.append('[');
// loops over the list appending the value of each objects getAsJson()
builder.append(']');
return builder.toString();
}
Run Code Online (Sandbox Code Playgroud)
问题是我有大约6种方法(并且还在增长),除了不同的DAO查询外,它们看起来完全一样.我的想法是创建一个只有getAsJson()方法定义的接口,让每个bean实现它,然后在servlet中有另一个方法来接受实现该接口的对象.结束看起来像这样:
public Interface JsonEnabled {
public String getAsJson();
}
public class User implements JsonEnabled {
....
@Override
public String getAsJson() {...}
}
public class TheServlet {
...
private String getUserListAsJson() {
List<User> userList = this.dao.getUsers();
return this.getListAsJson(userList);
}
private String getListAsJson(List<? implements JsonEnabled> list) { …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将列表框1中的选定项目移动到列表框2,反之亦然.我有两个按钮,>>
和<<
.当我在listbox1中选择项目然后单击>>
项目应该从listbox1移动到listbox2.
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
Run Code Online (Sandbox Code Playgroud) 这是标题DIV的CSS:
#header {
display: block;
float: left;
min-width: 100%;
min-height: 170px;
height: auto;
background-color: #030309;
background-color: #191718;
position:fixed;
margin-top: -210px;
z-index: 999999;
opacity:.76;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
top: 0;
}
Run Code Online (Sandbox Code Playgroud)
这是为了悬停状态:
#header:hover {
display:block;
opacity:1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
Run Code Online (Sandbox Code Playgroud)
我想做什么,所以标题默认隐藏,并在它悬停时出现.我试着添加display: none
上#header
和display:block;
上 …