2012-07-03 4 views
0

XML 데이터 삽입을 위해 아래 코드를 수정해야합니다.웹 서비스의 응답 XML에서 XML 데이터 삽입

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = factory.newDocumentBuilder(); 
InputSource inStream = new InputSource(); 
inStream.setCharacterStream(new StringReader(xmlFromWebService)); 
Document doc = db.parse(inStream); // reported at this line by a code audit tool 
doc.getDocumentElement().normalize(); 

어떻게 고칠 수 있습니까? 누구든지 어떤 제안이 있으십니까?

+0

예. 걱정됩니다. 또한 모듈에서 솔루션을 찾아 구현 한 후 각 질문에 올바른 솔루션을 게시했습니다. 나는 그것에 대해 알지 못했다. –

+0

어이 지금이 하나에 해결책을 줄 수 있습니다 !!!!! –

답변

1

XML 데이터 삽입을 방지하기 위해 주어진 XSD에 대한 XML 유효성 검사와 관련이 있다고 생각합니다. 다음과 같이 코드를 수정하는 것이 좋습니다.

try { 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    factory.setValidating(true); 

    factory.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
         "http://www.w3.org/2001/XMLSchema"); 
    factory.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 
         "file:<your_xsd_file>"); 

    DocumentBuilder builder = factory.newDocumentBuilder(); 
    InputSource inStream = new InputSource(); 
    inStream.setCharacterStream(new StringReader(xmlFromWebService)); 
    Document document = builder.parse(inStream); 

} catch (ParserConfigurationException e) { 
    e.printStackTrace(); 
} catch (SAXException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

힌트를 얻으시기 바랍니다.

+0

나중에 나는 point.ahave도 같은 연구 개발을 마쳤다. 이 프로퍼티가 어떤 속성을 설정하는지 말해 주실 수 있습니까? 나는 실제 이름으로 setAttribute를 얻고있다. 같은 방식으로 작동할까요? 또한 이러한 속성의 가치는 무엇입니까? 또한 모든 것들이 documentBuilderFactory에 의해 수행되는 작업을 얻는 방법은 무엇입니까? –

+0

@ R.K.R : 1. SchemaLanguage & Schema Source (그 이유는 두 번째 속성 값을 의도적으로 으로 유지했기 때문입니다). 2. setAttribute도 작동합니다. 내 코드에서이 두 줄을 읽고 값에 무엇을 사용해야하는지 이해할 수 있습니다. 3. 질문의 마지막 부분이 무엇을 의미하는지 이해하지 못했습니다. – Sujay

+0

나는 documentBuilderfactory가 모든 물건 xml을 검증하는 방법을 의미합니까? 내가 그걸로 끝나면 질문에 대한 모든 해결책을 발견하고 데모 솔루션을 구현하고 클라이언트가 승인하도록 할 것입니다. 고마워요 !!!!!! 1 –