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