XML 내 XML 내가 헤더 정보를 분석하려고이에 있어요 따르고XML이
import java.io.File;
import java.io.FileInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
public class XPathEvaluator {
/*
* ServiceGroup serviceGroup = new ServiceGroup(); List<Service>
* requiredServices = new ArrayList<Service>(); List<Service>
* recommandedServices = new ArrayList<Service>(); Service service = new
* Service();
*/
public void evaluateDocument(File xmlDocument) {
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String requiredServicesExpression = "/Envelope/Header";
InputSource requiredServicesInputSource = new InputSource(
new FileInputStream(xmlDocument));
DTMNodeList requiredServicesNodes = (DTMNodeList) xPath.evaluate(
requiredServicesExpression, requiredServicesInputSource,
XPathConstants.NODESET);
System.out.println(requiredServicesNodes.getLength());
NodeList requiredNodeList = (NodeList) requiredServicesNodes;
for (int i = 0; i < requiredNodeList.getLength(); i++) {
Node node = requiredNodeList.item(i);
System.out.println(node.getChildNodes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
XPathEvaluator evaluator = new XPathEvaluator();
File xmlDocument = new File("d://eva.xml");
evaluator.evaluateDocument(xmlDocument);
}
}
분석 가치없는 아이 구문 분석 구문 분석을 소중히하지
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<User id="MAKRISH"/>
<Request-Id id="1"/>
<Type name="Response"/>
<Application-Source name="vss" version="1.0"/>
<Application-Destination name="test" />
<Outgo-Timestamp date="2012-08-24" time="14:50:00"/>
<DealerCode>08301</DealerCode>
<Market>00000</Market>
</Header>
</Envelope>
내가 얻을 수 아니다 머리글 자식 어떻게 getchildNodes 메서드에서 null을주는 그들을 얻을 수 있습니다. 나는 많은 해결책을 찾지 만 어떤 것을 얻는다.
다음 구문 분석 태그에 따라 DOM 이루어집니다
getChildNodes가 null을 반환하는지 확인하십시오. [Node for API] (http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes%28%29)에 따르면 NodeList가 반환됩니다. 아이가없는 경우는 하늘입니다. null를 돌려 주어서는 안됩니다. – Bobulous
null을 반환하지 않습니다. 그러나 System.out.println (node.getChildNodes())은 OP를 혼란스럽게하는 "Header : null"을 인쇄하고 있습니다. –
에 정확히 파서를 언급하고 소스와 Xpath에 DOM을 사용하고 SAX로 태그를 사용했다면 – gks