这是我的XML文件:
<applications>
<apps>
<appname>MYapp</appname>
<server>qwe</server>
<port>1234</port>
<uname>system</uname>
<passwd>security</passwd>
</apps>
<apps>
<appname>tyas</appname>
<server>qwewe</server>
<port>1235</port>
<uname>asd</uname>
<passwd>wetry</passwd>
</apps>
</applications>
这是我的Java代码:
package trial;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class parsing {
public static void main(String args[])
{
try {
File applications = new File("appdetails.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(applications);
doc.getDocumentElement().normalize();
System.out.println("application details:");
NodeList nodes = doc.getElementsByTagName("apps");
System.out.println("===================");
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
System.out.println("app name: " + getValue("appname", element));
String uname = getValue("uname",element);
String host = getValue("server",element);
String passwd = getValue("passwd",element);
String clu = getValue("clustername",element);
String path = getValue("path",element);
String cmd = getValue("cmd" ,element);
String port = getValue("port" ,element);
String user= getValue("user",element);
System.out.println(user);
String pswd= getValue("pswd" ,element);
System.out.println(pswd);
}}} catch (Exception ex) {ex.printStackTrace();
}
}
private static String getValue(String tag, Element element)
{
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}
我得到的输出遍历两个节点。如何修改此代码以仅显示我选择的节点的详细信息?如果我选择MYapp,它应该只显示Myapp的详细信息。
答案 0 :(得分:2)
更改
if (node.getNodeType() == Node.ELEMENT_NODE)
到
if (node.getNodeType() == Node.ELEMENT_NODE && "MYapp".equals(getValue("appname", element)))
答案 1 :(得分:2)
使用JAXB读取XML。它会让你的生活更轻松。
public class Parsing {
public static void main(String args[]) {
String xml = "<applications>\n" +
"<apps>\n" +
"<appname>MYapp</appname>\n" +
"<server>qwe</server>\n" +
"<port>1234</port>\n" +
"<uname>system</uname>\n" +
"<passwd>security</passwd>\n" +
"</apps>\n" +
" <apps>\n" +
"<appname>tyas</appname>\n" +
"<server>qwewe</server>\n" +
"<port>1235</port>\n" +
"<uname>asd</uname>\n" +
"<passwd>wetry</passwd>\n" +
"</apps>\n" +
"</applications>";
Applications applications = JAXB.unmarshal(new StringReader(xml), Applications.class);
Optional<Apps> appsOptional = applications.getApps().stream().filter(app -> app.getAppname().equals("MYapp")).findFirst();
if(appsOptional.isPresent()) {
Apps app = appsOptional.get();
System.out.println("Got MyAPP object: "+ app.getUname());
}
}
}
class Applications {
@XmlElement
private List<Apps> apps;
public List<Apps> getApps() {
return apps;
}
}
class Apps {
@XmlElement
private String appname;
@XmlElement
private String server;
@XmlElement
private String port;
@XmlElement
private String uname;
@XmlElement
private String passwd;
public String getAppname() {
return appname;
}
public String getServer() {
return server;
}
public String getPort() {
return port;
}
public String getUname() {
return uname;
}
public String getPasswd() {
return passwd;
}
}
答案 2 :(得分:2)
您可以使用XPath:
XPath path = XPathFactory.newInstance().newXPath();
NodeList nl=(NodeList)path.evaluate("applications/apps[appname = 'MYapp']", doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
System.out.println(nl.item(i).getNodeName());
}
答案 3 :(得分:0)
获取输入以阅读MyApp
等应用。
阅读时如果appname => node.getValue().equals("MyApp")
继续其他中断并查看下一步。如果XML结束显示错误消息。