XML 到哈希表
目前我有下面的代码将数据放入哈希中。 我的问题:我必须在 !!!SOMETHING!!! 部分放入哪个值。 该代码只需读取一个元素标签并将其值插入哈希表中。
public void ReadXML(){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileout);
doc.getDocumentElement().normalize();
Hashtable hash = new Hashtable();
NodeList dataNodes = doc.getElementsByTagName("DataArea");
// getChildNodes().item(0).getChildNodes();
Element root = doc.getDocumentElement();
String dataNodeIndex = root.toString();
System.out.println("");
for (int dataNodeIndex1 = 0; dataNodeIndex1 < dataNodes.getLength(); dataNodeIndex1++)
{
Node nodeName = dataNodes.item(dataNodeIndex1);
if (nodeName.getNodeType() == Node.ELEMENT_NODE) {
Element elementName = (Element) nodeName;
NodeList elementNameList = elementName.getElementsByTagName(elementtag1);
Element elementName2 = (Element) elementNameList.item(0);
NodeList nameElement = elementName2.getChildNodes();
System.out.println("NodeContent: " + ((Node) nameElement.item(0)).getNodeValue());
}
hash.put(elementtag1, !!!SOMETHING!!!);
System.out.println(hash);
}
}
catch(Exception e){
e.printStackTrace();
}
}
Currently I have the code below to put the data in a Hash.
My question: which value do i have to put in the part of !!!SOMETHING!!!.
The code only has to read one elementtag and insert it's value in the hashtable.
public void ReadXML(){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileout);
doc.getDocumentElement().normalize();
Hashtable hash = new Hashtable();
NodeList dataNodes = doc.getElementsByTagName("DataArea");
// getChildNodes().item(0).getChildNodes();
Element root = doc.getDocumentElement();
String dataNodeIndex = root.toString();
System.out.println("");
for (int dataNodeIndex1 = 0; dataNodeIndex1 < dataNodes.getLength(); dataNodeIndex1++)
{
Node nodeName = dataNodes.item(dataNodeIndex1);
if (nodeName.getNodeType() == Node.ELEMENT_NODE) {
Element elementName = (Element) nodeName;
NodeList elementNameList = elementName.getElementsByTagName(elementtag1);
Element elementName2 = (Element) elementNameList.item(0);
NodeList nameElement = elementName2.getChildNodes();
System.out.println("NodeContent: " + ((Node) nameElement.item(0)).getNodeValue());
}
hash.put(elementtag1, !!!SOMETHING!!!);
System.out.println(hash);
}
}
catch(Exception e){
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用我找到的这些方法:
像这样使用它:
检查一下:
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
和
如何使用 Java 检索 XML 的元素值?
You should use these method that i found :
use it like this :
Check it out :
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
and
How to retrieve element value of XML using Java?
您为此操作选择了不正确的集合类型,如果您想将元素标记值保存在
Set
中,是的,最好使用HashSet
但要实现HashSet 大约是您尝试做的,因此
Set
的值像键一样放入HashMap
中,但您可以使用另一个集合,例如List
、队列
,堆栈
尝试找到更好的 为你。也许
SAX
对您来说会更好DOM
...You have chosen incorrect collection type for this operation, if you wanna save your element tag values in
Set
yes it is better to useHashSet
but implementation ofHashSet
approximately you try to do, so values ofSet
puts intoHashMap
like keys, but you can use another collection likeList
,Queue
,Stack
try to find better for you.And maybe
SAX
will be betterDOM
for you ...为了使事情变得更简单、更强大,您可以使用 Properties 相反,它具有 Hashtable(它实际上扩展了它),并且可以导入和导出 XML(请参阅
loadFromXML
和storeToXML
方法)。请参阅 http://www.ibm.com/developerworks/java/详细信息请参见library/j-tiger02254/index.html。To make things easier and more robust, you could use a Properties instead, which has an underlying implementation of a Hashtable (it actually extends it) and can import and export to/from XML (see
loadFromXML
andstoreToXML
methods). See http://www.ibm.com/developerworks/java/library/j-tiger02254/index.html for details.