2017-12-29 2 views
0

첫 번째 두 서비스 작성자의 이름을 가져오고 싶습니다. Java get XML 노드

난 다음 XML 예제가 있습니다

<ServiceSchema:id>10</ServiceSchema:id> 
<ServiceSchema:name>Service 10</ServiceSchema:name> 
<ServiceSchema:authorInfos> 
    <ServiceSchema:authorInfo> 
     <ServiceSchema:id>1</ServiceSchema:id> 
     <ServiceSchema:name>Service Author 1</ServiceSchema:name> 
    <ServiceSchema:authorInfo> 
    <ServiceSchema:authorInfo> 
     <ServiceSchema:id>2</ServiceSchema:id> 
     <ServiceSchema:name>Service Author 2</ServiceSchema:name> 
    <ServiceSchema:authorInfo> 
</ServiceSchema:authorInfos> 

<ServiceSchema:requirements> 
    <ServiceSchema:requirement> 
     <ServiceSchema:id>1</ServiceSchema:id> 
     <ServiceSchema:name>Requirement 1</ServiceSchema:name> 
     <ServiceSchema:authorInfos> 
      <ServiceSchema:authorInfo> 
       <ServiceSchema:id>5</ServiceSchema:id> 
       <ServiceSchema:name> Requirement Author 5</ServiceSchema:name> 
      <ServiceSchema:authorInfo> 
     </ServiceSchema:authorInfos> 
    </ServiceSchema:requirement> 
..... 
나는 다음과 같은 코드를 사용하여 모든 이름 노드 얻을 수

:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputSource inputSource = new InputSource(new ByteArrayInputStream (xml.getBytes("utf-8"))); 
Document xmldocument = db.parse(inputSource); 
NodeList nodeList = xmldocument.getElementsByTagNameNS("*", "name"); 

을하지만 난이의 이름 만 활용하는 방법을 모른다 외부 저자.

감사합니다.

업데이트 :

내가 완전히 수행하려고하는 것은 : 나는 서로 다른 노드에 대해 서로 다른 xml 파일을 검색 할 수 있습니다. 위의 xml 파일은 예제로 사용되었습니다. 예를 들어

:

searchElementsXml = new String[]{"name", "id", "version", "description", "keywords", "authorInfos.authorInfo.name", "status"}; 

나는 많은 값이 발생할 수있는 필드가 있습니다. 저자 정보처럼. 좋은 해결책이 거기가

NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos:authorinfo:name"); 

:

그래서 내가 뭔가를해야합니까?

+0

당신이 출력 노드로 기대 정확히 추가하시기 바랍니다에게 있습니다. – MrSmith42

답변

0

내가 원하는 것을 정확히 이해했다면 나는 모든 저자 정보 (ServiceSchema : authorInfo)를 얻고 각 authorInfo에 대한 이름 노드를 얻는 것이 좋습니다.

+0

나는이 질문을 업데이트했다. 다시 볼 수 있다면 좋을 것이다. – Simon

+0

XPATH를 사용해야합니다. 이 자습서를 참조하십시오 : http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/ – Ananta

0

XML이 올바르므로 ServiceSchema : authorInfo 태그가 닫히고 있습니다.
찾고있는 내용을 업데이트하십시오. authors 정보를 얻으려면 아래 코드를 사용하십시오 NodeList nodes = doc.getElementsByTagName ("ServiceSchema : authorInfos");

그녀는 세부 실행 코드

File fXmlFile = new File("/Users/Downloads/test.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 
doc.getDocumentElement().normalize(); 
NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos"); 
for (int temp = 0; temp < nodes.getLength(); temp++) { 
    Node nNode = nodes.item(temp); 
    System.out.println(" Current Author Element :" + nNode.getNodeName()); 
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
     Element eElement = (Element) nNode; 
     System.out.println("Author Info Name : " + eElement.getElementsByTagName("ServiceSchema:authorInfo").getLength()); 
    } 
} 
+0

질문을 업데이트했습니다. 다시 볼 수 있으면 좋을 것입니다. – Simon