@Test public void xmlPathCanExtractNodeFromSoap() throws Exception { // Given String soap = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<env:Envelope \n" + " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n" + " xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" \n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + " <env:Header/>\n" + "\n" + "<env:Body>\n" + " <n1:importProjectResponse \n" + " xmlns:n1=\"n1\" \n" + " xmlns:n2=\"n2\" \n" + " xsi:type=\"n2:ArrayOfProjectImportResultCode\">\n" + " <n2:ProjectImportResultCode>\n" + " <n2:code>1</n2:code>\n" + " <n2:message>Project 'test1' import was successful.</n2:message>\n" + " </n2:ProjectImportResultCode>\n" + " </n1:importProjectResponse>\n" + "</env:Body></env:Envelope>"; // When XmlPath xmlPath = new XmlPath(soap); Node node = xmlPath.getNode("Envelope"); // Then assertThat( node.<String>getPath("Body.importProjectResponse.ProjectImportResultCode.code"), equalTo("1")); }
@Test public void multipleGetsWithOneInstanceOfXmlPath() throws Exception { final XmlPath xmlPath = new XmlPath(XML); assertThat(xmlPath.getInt("shopping.category.item.size()"), equalTo(5)); assertThat( xmlPath.getList("shopping.category.item.children().list()", String.class), hasItem("Pens")); }
@Test public void doesntNeedToEscapeListsWithHyphenWithoutBrackets() throws Exception { // Given String xml = "<root><some-list>one</some-list><some-list>two</some-list></root>"; // When XmlPath xmlPath = XmlPath.from(xml); // Then assertThat(xmlPath.getString("root.some-list[0]"), equalTo("one")); }
@Test public void disableDtdValidationWorks() throws Exception { // Given String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE LegacyService SYSTEM \"http://example.com/dtd/NonExistent.dtd\">\n" + "<LegacyService>Text</LegacyService>"; // When final XmlPath xmlPath = new XmlPath(xml).using(xmlPathConfig().with().disableLoadingOfExternalDtd()); // Then assertThat(xmlPath.getString("LegacyService"), equalTo("Text")); }
@Test public void setting_features_works() throws Exception { // Given String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE LegacyService SYSTEM \"http://example.com/dtd/NonExistent.dtd\">\n" + "<LegacyService>Text</LegacyService>"; Map<String, Boolean> features = new HashMap<String, Boolean>(); features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); features.put("http://xml.org/sax/features/namespaces", false); // When final XmlPath xmlPath = new XmlPath(xml).using(xmlPathConfig().with().features(features)); // Then assertThat(xmlPath.getString("LegacyService"), equalTo("Text")); }
@Test public void setting_feature_works() throws Exception { // Given String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE LegacyService SYSTEM \"http://example.com/dtd/NonExistent.dtd\">\n" + "<LegacyService>Text</LegacyService>"; // When final XmlPath xmlPath = new XmlPath(xml) .using( xmlPathConfig() .with() .feature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false) .feature("http://apache.org/xml/features/disallow-doctype-decl", false)); // Then assertThat(xmlPath.getString("LegacyService"), equalTo("Text")); }
@Test public void rootPathEndingWithDot() throws Exception { final XmlPath xmlPath = new XmlPath(XML).setRoot("shopping.category.item."); assertThat(xmlPath.getInt("size()"), equalTo(5)); assertThat(xmlPath.getList("children().list()", String.class), hasItem("Pens")); }