xml 노드에서 텍스트를 가져 오려고합니다. 코드가 노드를 인식하는 것 같습니다. 이 코드 문자열 L = "노드 길이 :"+ nList.getLength() + "텍스트 :"+ nList.item (0) .toString(); jTextArea1.setText (L);null 노드 예외를 반환하는 xml 노드의 getTextContent
반환 : 코드는 노드를 찾을 수 있지만, 값을받지처럼 Node Length: 1 Text: [CompanyName: null]
그래서 것 같다. 여기에 전체 코드 블록이 있습니다 (이것은 처음으로 올린 글이므로이 형식을 바란다는 희망이 있습니다!). FOR 루프는 값을 잡아해야하지만 NULL 포인터 예외를 던지고있다 :
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//Build Document
Document xdocument = builder.parse(new File("request.xml"));
xdocument.getDocumentElement().normalize();
NodeList nList = xdocument.getElementsByTagName("CompanyName");
//String L = "Node Length: " + nList.getLength()+ " Text: " + nList.item(0).toString();
//jTextArea1.setText(L);
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
String nodetxt= "Company : " + eElement.getElementsByTagName("CompanyName").item(0).getTextContent() ;
jTextArea1.setText(nodetxt) ;
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(TechKnowPOSGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
을 여기에 XML 파일입니다
<?xml version="1.0" encoding="utf-8"?>
<RunReportQueryAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CompanyName>Techknow</CompanyName>
<IntegrationLoginId>cwapitest</IntegrationLoginId>
<IntegrationPassword>cwtest123</IntegrationPassword>
<ReportName>Company</ReportName>
<!-- <Conditions></Conditions> -->
<!-- <Limit>10</Limit> -->
<!-- <Skip></Skip> -->
<!-- <OrderBy></OrderBy> -->
</RunReportQueryAction>
어떤 도움이 크게 감사됩니다. 이 라인에
완벽하게 작동한다. 환자 답변에 감사드립니다. 그것은 "googled"코드 였고 더 잘 이해하도록 도와주었습니다. – creed