XML文件如下:
<?xml version="1.0" encoding="shift_jis"?>
<IV_Key>
<IV>20050630 abcdefgh</IV>
<IV>20050630 abcdefgi</IV>
<DESCryptionKey>20050630 abcdefgh</DESCryptionKey>
<DESCryptionKey>20050631 abcdefgh</DESCryptionKey>
<TripleDESCryptionKey>20050620 123456789012345678901234</TripleDESCryptionKey>
<TripleDESCryptionKey>20050621 123456789012345678901234</TripleDESCryptionKey>
</IV_Key>程序如下:
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 Test {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse("IV_Key.xml");
NodeList nl = xmlDoc.getElementsByTagName("IV");
for (int i = 0; i < nl.getLength(); i++) {
Node nd = nl.item(i);
System.out.println(nd.getNodeName() + " : " + nd.getNodeValue());
}
}}运行结果如下:
IV : null
IV : null为何此处得到的IV值为null?
<?xml version="1.0" encoding="shift_jis"?>
<IV_Key>
<IV>20050630 abcdefgh</IV>
<IV>20050630 abcdefgi</IV>
<DESCryptionKey>20050630 abcdefgh</DESCryptionKey>
<DESCryptionKey>20050631 abcdefgh</DESCryptionKey>
<TripleDESCryptionKey>20050620 123456789012345678901234</TripleDESCryptionKey>
<TripleDESCryptionKey>20050621 123456789012345678901234</TripleDESCryptionKey>
</IV_Key>程序如下:
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 Test {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse("IV_Key.xml");
NodeList nl = xmlDoc.getElementsByTagName("IV");
for (int i = 0; i < nl.getLength(); i++) {
Node nd = nl.item(i);
System.out.println(nd.getNodeName() + " : " + nd.getNodeValue());
}
}}运行结果如下:
IV : null
IV : null为何此处得到的IV值为null?
Node nd = nl.item(i);
System.out.println(nd.getNodeName() + " : " + nd.getNodeValue());
}
改为:
for (int i = 0; i < nl.getLength(); i++) {
Node nd = nl.item(i);
Text text = (Text) nd.getFirstChild();
System.out.println(nd.getNodeName() + " : " + text.getNodeValue());
}文本也算是一个节点,只不过它是一个文本类型的节点
对于这个xml:
<?xml version="1.0" encoding="shift_jis"?>
<VALIDKEY>
<D20050601>
<IV>11111111</IV>
<DESKEY>11111111</DESKEY>
<TRIPLEDESKEY>111111111111111111111111</TRIPLEDESKEY>
</D20050601>
<D20050602>
<IV>22222222</IV>
<DESKEY>22222222</DESKEY>
<TRIPLEDESKEY>222222222222222222222222</TRIPLEDESKEY>
</D20050602>
<D20050608>
<IV>33333333</IV>
<DESKEY>33333333</DESKEY>
<TRIPLEDESKEY>333333333333333333333333</TRIPLEDESKEY>
</D20050608>
</VALIDKEY>为何我这样解析不对?
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse("CryptionDataKey.xml");
Node rootNode = xmlDoc.getDocumentElement();
NodeList dateNodeList = rootNode.getChildNodes();
for (int i = 0; i < dateNodeList.getLength(); i++) {
Node keyNode = dateNodeList.item(i);
NodeList keyNodeList = keyNode.getChildNodes();
for (int j = 0; j < keyNodeList.getLength(); j++) {
Node nde = keyNodeList.item(j);
Text text = (Text)nde.getFirstChild();
System.out.println(nde.getNodeName() + " : " + text.getNodeValue());
}
}
}
Node keyNode = dateNodeList.item(i);
/**
* <VALIDKEY>的子元素有 7 个:
* <VALIDKEY>和<D20050601>之间有一个空格类型的子元素,<D20050601>是一个元素,
* <D20050601>和<D20050602>之间又有一个空格类型的子元素,于此类推,下面<IV>也有 7 个子因素,
* 情况是一样的。
*/
if(keyNode.getNodeType() == Node.ELEMENT_NODE){
System.out.println(keyNode.getNodeName());
NodeList keyNodeList = keyNode.getChildNodes();
for (int j = 0; j < keyNodeList.getLength(); j++) {
Node nde = keyNodeList.item(j);
if(nde.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element)nde;
Text text = (Text) element.getFirstChild();
System.out.println(" "+text.getNodeValue());
}
}
}
}