2013-07-05 7 views
0

XML로 변환해야하는 여러 도서의 참조가 있습니다.
이 작업을 위해 Java로 응용 프로그램을 만들고 싶습니다.
책의 참고 문헌을 XML로 변환하는 방법은 무엇입니까?

책의 참조 :

Schulz V, Hansel R, Tyler VE. Rational phytotherapy: a physician's guide to herbal 
medicine. 3rd ed., fully rev. and expand. Berlin: Springer; c1998. 306 p. 


XML :

<element-citation publication-type="book" publication-format="print"> 
    <name> 
     <surname>Schulz</surname> 
     <given-names>V</given-names> 
    </name> 
    <name> 
     <surname>Hansel</surname> 
     <given-names>R</given-names> 
    </name> 
    <name> 
     <surname>Tyler</surname> 
     <given-names>VE</given-names> 
    </name> 
    <source>Rational phytotherapy: a physician's guide to herbal medicine</source> 
    <edition>3rd ed., fully rev. and expand</edition> 
    <publisher-loc>Berlin</publisher-loc> 
    <publisher-name>Springer</publisher-name> 
    <year>c1998</year> 
    <size units="page">306 p</size> 
</element-citation> 


어떻게 XML 형식으로 책의 참조를 변환하는?
무엇을 제안합니까?

+0

이러한 참조의 구조는 무엇입니까? 그것은 들판들과이 들판들의 주문을 가지고 있습니까? 필드를 어떻게 인식 할 수 있습니까? –

+0

어떤 도서 참조가 명확하지 않은가요? –

+2

개인적으로 JAXB를 사용하여 Java로 변환하면 입력 참조의 거의 구조화되지 않은 특성이 가장 큰 도전 과제가 될 것이라고 생각합니다. – fvu

답변

0

당신은 자바, 무딘, 간단한 솔루션 (자바 7)에 경험이되지 않을 수 있습니다으로 :

  • 텍스트로 XML을 작성;
  • String.split(regex) (Scanner)으로 구문 분석 할 수 있습니다.

마음은, 특수 문자 bookref 텍스트 < > & " '&lt; &gt; &amp; &quot; &apos;로 대체해야 할 수도 있습니다.

String bookRef = "Schulz V, Hansel R, Tyler VE. Rational phytotherapy: a physician's guide to herbal " 
     + "medicine. 3rd ed., fully rev. and expand. Berlin: Springer; c1998. 306 p."; 

File file = new File("D:/dev/xml-part.txt"); 
final String TAB = " "; 
try (PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")))) { 
    out.println(TAB + "<element-citation publication-type=\"book\" publication-format=\"print\">"); 

    String[] lines = bookRef.split("\\.\\s*"); 

    String names = lines[0]; 
    String[] nameArray = names.split(",\\s*"); 
    for (String name : nameArray) { 
     String[] nameParts = name.split(" +", 2); 
     out.println(TAB + TAB + "<name>"); 
     out.println(TAB + TAB + TAB + "<surname>" + nameParts[0] + "</surname>"); 
     out.println(TAB + TAB + TAB + "<given-name>" + nameParts[1] + "</given-name>"); 
     out.println(TAB + TAB + "</name>"); 
    } 
    out.println(TAB + TAB + "<source>" + lines[1] + "</source>"); 
    ... 

    out.println(TAB + "</element-citation>"); 
} catch (FileNotFoundException | UnsupportedEncodingException ex) { 
    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
} 
2

예를 들어, JAXB를 사용하십시오.

  1. XML 형식으로 XSD을 가져 오십시오.
  2. XSD에서 자바 클래스 생성 - 자세한 내용은 here을 참조하십시오.
  3. 입력 파일을 구문 분석하고 생성 된 클래스의 도움으로 트리를 작성하는 간단한 프로그램을 구현하십시오. 이것은 입력에 따라 사소하거나 매우 어려울 수 있습니다.
  4. 결과 직렬화 - here 참조.

편집 : 윱 Eggen에 의해 암시로 당신이 대신 1-3 단계의 주석을 사용할 수 있습니다. 이것은 아마도 상황을 더 단순하게 만듭니다. 방법을 참조하십시오 here.

+0

3 단계는 레코드 구조에 대해 더 알지 못하는 한 어려운 단계입니다. –

+1

XSD 대신 주석을 사용할 수 있습니다. 그렇다면 그것은 매우 간단합니다. –

+1

@Tichodroma Yeap, 솔직히 말해서 나에게 무료 텍스트처럼 보인다. 그러나 이것이 문제인 경우 문제는 잘못된 것 같습니다. XML을 전혀 다루지 않아야합니다 :-) –