2013年11月4日星期一

java XML problem

First Look xml
-----------------

<?xml version="1.0" encoding="GB2312" standalone="no"?>
<公司人员 xmlns:p1="first://www.dlrin.com" xmlns:p2="second://www.dlrin.com">
<p1:张三>1989年出生,毕业于上海交通大学</p1:张三>
<p2:张三>1972年出生,毕业于中国科学技术大学</p2:张三>
<李四>1985年出生,毕业于北京大学</李四>
</公司人员>

---------------------
Then take a look at my Java code :

public class test {
public static void main(String[] args) throws Exception{
DocumentBuilderFactory docbudfact = DocumentBuilderFactory.newInstance() ;
DocumentBuilder docbud = docbudfact.newDocumentBuilder() ;
Document doc = docbud.parse(new File("a.xml"));
Element element = doc.getDocumentElement();
NodeList nodelist = element.getElementsByTagNameNS("second://www.dlrin.com", "张三");
findNodeMessage(nodelist);
nodelist = element.getElementsByTagName("李四");
findNodeMessage(nodelist);
}

public static void findNodeMessage(NodeList nodelist) {
int size = nodelist.getLength() ;
for(int k=0 ; k<size ; k++ ){
Node node = nodelist.item(k) ;
String name = node.getNodeName() ;
int index = name.indexOf(":");
if(index != -1){
name = name.substring(index+1) ;
}
String content = node.getTextContent() ;
content = content.trim() ;
System.out.println(name+":"+content);
}
}
}


run , the result is : John Doe : born in 1985 , graduated from Peking University

questions:
Why connotes the information does not appear, I can use a namespace query method to query the local name of the node , ah, how the seating of the result is still not out ?
------ Solution ---------------------------------------- ----


Joe Smith
There go somewhere . .
------ Solution ---------------------------------------- ----
DocumentBuilderFactory docbudfact = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder docbud = docbudfact.newDocumentBuilder();
Document doc = docbud.parse(new File("a.xml"));
Element element = doc.getDocumentElement();
NodeList nodelist = element.getElementsByTagNameNS(
"second://www.dlrin.com", "p1:张三");
//"second://www.dlrin.com", "p2:张三");
nodelist = element.getElementsByTagName("张三");
findNodeMessage(nodelist);
nodelist = element.getElementsByTagName("李四");
findNodeMessage(nodelist);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

------ Solution ------------------------------ --------------
//我测试了 可以得到张三
//1989年出生,毕业于上海交通大学
NodeList nodelist = element.getElementsByTagNameNS( "second://www.dlrin.com", "p1:张三");
//张三>1972年出生,毕业于中国科学技术大学
NodeList nodelist = element.getElementsByTagNameNS( "second://www.dlrin.com", "p2:张三");                   
                  

------ Solution -------------------- ------------------------
you less a statement ! docbudfact.setNamespaceAware (true);
------ For reference only ------------------------------- --------


Oh, what difference , please advise ! [Note : I have here two different domain name connotes , but I just use the domain name lookup ah ]
------ For reference only ----------------- ----------------------


using your method , and can only get information about John Doe .
I want to know "second :/ / www.dlrin.com" John Smith 's information , how to do it ?
------ For reference only -------------------------------------- -


Indeed, buttoned up , thank you ...

没有评论:

发表评论