XML

2016-11-17 2 views
0

의 텍스트 콘텐츠를하는 방법 나는 다음과 같은 코드가 있습니다XML

<ishfields> 
    <ishfield name="FTITLE" level="logical">3* Family map</ishfield> 
    <ishfield name="FDESCRIPTION" level="logical">111</ishfield> 
    <ishfield name="FCHANGES" level="version" /> 
</ishfields> 

내가 필드 이름 = "FDESCRIPTION"의 텍스트 콘텐츠를하고자합니다.

심지어 컨텐츠 (111)

를 가져올 수 없습니다 나는 내가 getelementsbytagname() 많은 방법을 사용했다. 누구든지이 작업을 수행하는 방법을 도울 수 있습니까? 난 단지 전체 문서 노드를 가져옵니다 알고

try { 

String file="E:\\Repository\\17Nov_demo\\file.xml"; 
DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance(); 
DocumentBuilder documentbuilder =documentbuilderfactory.newDocumentBuilder(); 
Document doc=documentbuilder.parse(file); 

Element element=doc.getDocumentElement(); 
NodeList nodelist=element.getChildNodes(); 
for (int i = 0; i < nodelist.getLength(); i++) { 
    System.out.println(nodelist); 
} 

} 
catch(Exception e) 
{ 

} 

: 여기 내 자바 코드입니다. 하지만 xpath 또는 뭐든간에 해당 특정 노드로 이동하는 방법을 이해할 수 없습니다. 도와주세요!!

+1

자바 코드를 입력하십시오 – unicorn2

+0

JSoup을 (를) 한 번 보시겠습니까? http://stackoverflow.com/questions/9886531/how-to-parse-xml-with-jsoup – L01c

답변

0

JAXB을 사용할 수 있습니다. 어느 JAXBhere인지 알 수 있습니다.

지금 당신이이 같은 예를 들어 자바에서 ishfields 클래스를 지정해야합니다 :

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="ishfields") 
class Ishfields{ 
    List<Ishfield> fields; 
} 

그리고 물론 Ishfield 클래스 :

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="ishfield") 
class Ishfield{ 
    @XmlAttribute 
    String name; 
    @XmlAttribute 
    String level;  
} 

이제 Main 수업 시간에 당신이 시도 할 수 있습니다 :

import java.io.FileReader; 
import java.util.List; 

import javax.xml.bind.JAXB; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
//those are the all the imports you need. You can import all of them in Main if you also put here the two classes as inner. 
public class Main { 

    public static void main(String[] args) throws Exception { 
     Ishfields i = JAXB.unmarshal(new FileReader("yourXml.xml"), Ishfields.class); 
     System.out.println(r); 
     JAXB.marshal(r, System.out); 
    } 
} 

그리고 무엇이든 할 수 있습니다. 생성 된 객체로 원한다. 값, 이름, 레벨 등을 얻을 수 있습니다. xml이 (가) 파일에 있습니다. xmlString 인 경우 동일한 작업을 수행 할 수있는 방법이 있습니다.