Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java从xml文件中提取值_Java_Xml_Spring Boot_For Xml Path - Fatal编程技术网

使用Java从xml文件中提取值

使用Java从xml文件中提取值,java,xml,spring-boot,for-xml-path,Java,Xml,Spring Boot,For Xml Path,这是我的响应包含XML文件,我想从这个XML响应中检索bEntityID=“328” <?xml version="1.0" encoding="UTF-8"?> <ns2:aResponse xmlns:ns2="http://www.***.com/F1/F2/F3/2011-09-11"> <createBEntityResponse bEntityID="328" /> </ns2:aResponse> 从这个响应中获取BEnt

这是我的响应包含XML文件,我想从这个XML响应中检索
bEntityID=“328”

  <?xml version="1.0" encoding="UTF-8"?>
<ns2:aResponse xmlns:ns2="http://www.***.com/F1/F2/F3/2011-09-11">
   <createBEntityResponse bEntityID="328" />
</ns2:aResponse>

从这个响应中获取
BEntityID
有什么建议吗?

虽然我不建议使用下面的方法来获取元素值,但是如果您非常想获取,请尝试下面的代码:

public class xmlValue {
    public static void main(String[] args) {
        String xml = "<ns2:aResponse xmlns:ns2=\"http://www.***.com/F1/F2/F3/2011-09-11\">\n" +
                "   <createBEntityResponse bEntityID=\"328\" />\n" +
                "</ns2:aResponse>";
        System.out.println(getTagValue(xml,"createBEntityResponse bEntityID"));
    }
    public static  String  getTagValue(String xml, String tagName){
         String [] s;
         s = xml.split("createBEntityResponse bEntityID");
         String [] valuesBetweenQuotes = s[1].split("\"");
         return  valuesBetweenQuotes[1];
    }
    }

我认为最好的方法是将xml反序列化为类似pojo的格式,然后获取值

entityResponse.getEntityId();

我尝试使用相同的XML文件,并能够使用以下代码获得bEntityId的值。希望能有帮助

@Test
public void xmlPathTests() {
    try {
    File xmlExample = new File(System.getProperty("user.dir"), "src/test/resources/Data1.xml");
    String xmlContent = FileUtils.readFileToString(xmlExample);

    XmlPath xmlPath = new XmlPath(xmlContent).setRoot("aResponse");

    System.out.println(" Entity ::"+xmlPath.getInt(("createBEntityResponse.@bEntityID"))); 

    assertEquals(328, xmlPath.getInt(("createBEntityResponse.@bEntityID")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

尝试了第一种方法…但是ArrayIndexOutOfBoundException@MangduYogii,请检查我添加的第一个代码。只需将其包装在某个函数中,它就会始终获取与标记匹配的值。这是一种非常复杂的Xml解析方法,所讨论的代码有一个很小的编码错误,如果得到纠正,则可以正常工作。不确定为什么在这种情况下需要库更改方法。此测试用例的驱动程序代码是什么。什么是实际的和预期的?这只是为了说明的目的,目的是回答用户的查询,无论如何都要更新代码片段。为指出这一点干杯。
Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); //parse the whole xml doc
for (Element e : doc.select("tagName")) {
    System.out.println(e); //select the specific tag and prints
}
entityResponse.getEntityId();
@Test
public void xmlPathTests() {
    try {
    File xmlExample = new File(System.getProperty("user.dir"), "src/test/resources/Data1.xml");
    String xmlContent = FileUtils.readFileToString(xmlExample);

    XmlPath xmlPath = new XmlPath(xmlContent).setRoot("aResponse");

    System.out.println(" Entity ::"+xmlPath.getInt(("createBEntityResponse.@bEntityID"))); 

    assertEquals(328, xmlPath.getInt(("createBEntityResponse.@bEntityID")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}