Java Tdd解析节点值错误
我试图创建一个测试来解析GoogleDistanceMatrix api的距离值。现在它都在一个字符串中,这样我就不必连接到api(发布了一个指向xml读出器的链接)。我在解析节点的值时遇到问题。我将所有内容都放在一个字符串中 这是我的测试,当我运行它时,结果是null,而不是64275Java Tdd解析节点值错误,java,xml,parsing,dom,tdd,Java,Xml,Parsing,Dom,Tdd,我试图创建一个测试来解析GoogleDistanceMatrix api的距离值。现在它都在一个字符串中,这样我就不必连接到api(发布了一个指向xml读出器的链接)。我在解析节点的值时遇到问题。我将所有内容都放在一个字符串中 这是我的测试,当我运行它时,结果是null,而不是64275 public class GoogleDistanceMatrixXMLTest { private static final String DISTANCE_XML_STRING = "<?xml
public class GoogleDistanceMatrixXMLTest {
private static final String DISTANCE_XML_STRING = "<?xml version=\"1.0\"?><DistanceMatrixResponse><status>OK</status><origin_address>Muncie, IN, USA</origin_address><destination_address>Miami, FL, USA</destination_address><row><element><status>OK</status><duration><value>64275</value><text>17 hours 51 mins</text></duration><distance><value>1961951</value><text>1,219 mi</text></distance></element></row></DistanceMatrixResponse>";
private Document document;
@Before
public void setUp() throws ParserConfigurationException,
SAXException, IOException{
InputSource source = createInputSourceFromSampleXMLData();
document = parseXMLFrom(source);
}
private InputSource createInputSourceFromSampleXMLData() {
StringReader stringReader = new StringReader(DISTANCE_XML_STRING);
return new InputSource(stringReader);
}
private Document parseXMLFrom(InputSource source) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
return documentBuilder.parse(source);
}
@Test
public void testRetrieveDurationValue() throws ParserConfigurationException, SAXException, IOException {
String durationChildNodeValue = document.getFirstChild().getChildNodes().item(3).getFirstChild().getChildNodes().item(1).getFirstChild().getNodeValue();
Assert.assertEquals("64275", durationChildNodeValue);
}
公共类GoogleDistanceMatrixXMLTest{
专用静态最终字符串距离\u XML\u String=“OKMuncie,IN,USAMAIM,FL,USAOK6427517小时51分钟19619511219英里”;
私人文件;
@以前
public void setUp()引发ParserConfiguration异常,
SAXException,IOException{
InputSource=createInputSourceFromSampleXMLData();
document=parseXMLFrom(源);
}
私有InputSource createInputSourceFromSampleXMLData(){
StringReader StringReader=新的StringReader(距离\u XML\u字符串);
返回新的输入源(stringReader);
}
私有文档parseXMLFrom(InputSource源代码)抛出ParserConfiguration异常、SAXException、IOException{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder DocumentBuilder=factory.newDocumentBuilder();
返回documentBuilder.parse(源代码);
}
@试验
public void testRetrieveDurationValue()引发ParserConfiguration异常、SAXException、IOException{
字符串durationChildNodeValue=document.getFirstChild().getChildNodes().item(3.getFirstChild().getChildNodes().item(1.getFirstChild().getNodeValue();
Assert.assertEquals(“64275”,durationChildNodeValue);
}
应该是
String durationChildNodeValue = document.getDocumentElement()...;
无法保证文档元素是第一个子元素。处理指令可能是第一个子元素
String durationChildNodeValue = document.getDocumentElement()...;