2013-02-14 2 views
0

SBA API의 xml 파일로 작업하려고합니다. 나는 문제가 그 이후 라인과 비슷한 라인을 다음에 생각 xml 파일을 관찰 한 후XML의 공백

[Fatal Error] loans_grants.dtd:3:22: White space is required before the attribute type in the declaration of attribute "CDATA" for element "count". Exception in thread "main" org.xml.sax.SAXParseException: White space is required before the attribute type in the declaration of attribute "CDATA" for element "count".

: 나는이 오류가 XPath는이 XML을 구문 분석 할 때

http://api.sba.gov/loans_grants/federal_and_state_financing_for/ny.xml

문제는 :

<grant_loans count="103">

<industry nil="true"/>

<state_name nil="true"/>

나는 count"103"nil"true" 사이에 공간이 있다면이 오류가 발생하지 것이라고 생각합니다. 전체 XML이 너무 커서 복사 한 부분을 복사하여 변경 한 다음 로컬 저장소에 저장했습니다. 그런 다음 오류없이 실행하고 구문 분석 할 수 있습니다. 방금 다음과 같은 공백을 넣었습니다.

<grant_loans count = "103"> 

내 프로그램에서 공백이 필요한 모든 장소에이 작업을 수행 한 다음 추가 구문 분석에 어떻게 사용할 수 있습니까?

필요한 경우 여기에 내 Java 코드를 게시 할 수 있지만 그 코드는 다른 xml 파일에서 작동하므로이 XML 파일에 문제가 있다고 생각합니다.

편집

자바 코드 세그먼트 :

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    DocumentBuilder builder; 
    Document doc = null; 
    XPathExpression expr = null; 
    builder = factory.newDocumentBuilder(); 
    doc = (Document) builder 
      .parse("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway&sensor=false"); 

    // Create a XPathFactory 
    XPathFactory xFactory = XPathFactory.newInstance(); 

    // Create a XPath object 
    XPath xpath = xFactory.newXPath(); 

    // Compile the XPath expression 
    expr = xpath.compile("//geometry/location/lat/text()"); 
    System.out.println("expr" + expr); 
    // Run the query and get a nodeset 
    Object result = expr.evaluate(doc, XPathConstants.NODESET); 

    // Cast the result to a DOM NodeList 
    NodeList nodes = (NodeList) result; 
    for (int i = 0; i < nodes.getLength(); i++) { 
     System.out.println(nodes.item(i).getNodeValue()); 
    } 

         //this works 
// 
// some other code 
// 
builder = factory.newDocumentBuilder(); 
    url = "http://api.sba.gov/loans_grants/federal_and_state_financing_for/ny.xml"; 
    doc = builder.parse(url); // problem occurs here 
    xFactory = XPathFactory.newInstance(); 

    // Create a XPath object 
    xpath = xFactory.newXPath(); 

    // Compile the XPath expression 
    expr = xpath.compile("//grant_loan/url/text()"); 
    result = expr.evaluate(doc, XPathConstants.NODESET); 

    // Cast the result to a DOM NodeList 
    nodes = (NodeList) result; 
    for (int i = 0; i < nodes.getLength(); i++) { 
     System.out.println(nodes.item(i).getNodeValue()); 
    } 

//other stuffs 
+0

xml에는 이와 같은 공백이 필요 없습니다. 귀하의 문제는 다른 곳에 있습니다 (또는 당신은 깨진 XML 파서를 사용하고 있습니다). – jtahlborn

답변

1

그것은 XML이 아니다. the DTD이 위로 올라 갔다고합니다. 오류가 시작될 때 loans_grants.dtd:3:22에 유의하십시오. "

<!ATTLIST element-name attribute-name attribute-type default-value> 

그것은 문자열을 보았다

<!ATTLIST count CDATA> 

아마 대신

<!ATTLIST grant_loans count CDATA #REQUIRED> 

이 오류가 ATTLISTproper format이라고 지적한다 읽어야합니다 : 라인 3 가리키는 것 CDATA "를 속성 이름이라고 가정하고 여전히 속성 유형을 얻으려는 대신에 ATTLIST의 끝을 발견했습니다. 그것이 공백을 예상하는 것에 대해 잠재적으로 혼란스러운 메시지를 전한 이유입니다.

xml 중 일부를 로컬에서 실행하도록 복사했을 때 대부분 DTD 선언을 중단 했으므로 문제가 해결됩니다.

+0

나는 본다. 그것을 지워 줘서 고마워. 그러나 여전히, 내가 그것을 어떻게 할 수있어서 내가 그걸로 일할 수 있는지. 나는 XML이나 SBA가 제공 한 DTD에 대한 통제권이 없다. 어떻게 든 그것을 사용해야합니다. –

+0

글쎄, 나는 시도 할 몇 가지를 생각할 수있다. 먼저, XML 파서를 속여서 DTD를 찾기 위해 다른 곳을 들여다 볼 수 있습니다. 그런 다음 올바르지 않은 것을로드하거나, 아니면 그냥 비어있는 것으로 만들고 유효성을 해제 할 수 있습니다.그렇지 않으면 파서에 전달하기 전에 doctype 선언을 XML 밖으로 제거 할 수 있습니다. –

+0

당신이 준 제안에 대해 몇 가지 출발점을 제시 할 수 있습니까? 지금 DTD를 제거하려고합니다. 좋은 생각입니까? –