gpt4 book ai didi

java - XML字符串解析Document应该有更大的值时却是0

转载 作者:行者123 更新时间:2023-12-01 17:59:47 25 4
gpt4 key购买 nike

嘿,我正在使用下面的代码来解析 XML 字符串收集该 XML 字符串中出现的所有 URL

String theReturnedResult = convertStreamToString(connection.getInputStream());
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

try {
builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new ByteArrayInputStream(theReturnedResult.getBytes()));

XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "jp06:members/jp06:role-assignments/jp06:role-assignment/jp06:url";
NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, xmlDocument, XPathConstants.NODESET);
//Document xmlDocument = builder.parse(new ByteArrayInputStream(theReturnedResult.getBytes()));

System.out.print(nodes.getLength());

for (int i = 0; i < nodes.getLength(); i++) {..........

XML 本身如下所示:

<?xml version="1.0" encoding="UTF-8"?><jp06:members xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/" xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/" jp:total-members="68">
<jp06:member>
<jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob</jp06:url>
<jp06:user-url>https://zzzzz.zzzzz.zz:9443/jts/users/Bob</jp06:user-url>
<jp06:role-assignments-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments</jp06:role-assignments-url>
<jp06:role-assignments>
<jp06:role-assignment>
<jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments/tester</jp06:url>
<jp06:role-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles/tester</jp06:role-url>
</jp06:role-assignment>
<jp06:role-assignment>
<jp06:url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob/role-assignments/default</jp06:url>
<jp06:role-url>https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles/default</jp06:role-url>
</jp06:role-assignment>
</jp06:role-assignments>
<jp06:role-assignments-editor-url>https://zzzzz.zzzzz.zz:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/members/Bob&amp;rolesUrl=https://zzzzz.zzzzz.zz:9443/qm/process/project-areas/_rutRzzzzzzzzzzzzzzzzz/roles</jp06:role-assignments-editor-url>
</jp06:member>
<jp06:member>.......

问题是我没有得到任何东西:

document [#document: null]

xmlDocument [#document: null]

变量theReturnedResult内部包含所有XML,所以我不确定为什么DocumentxmlDocumentnull ?

最佳答案

您的 XPath 似乎没有返回任何内容。您可以尝试使用(从 jp06:members 中删除 s):

//jp06:member/jp06:role-assignments/jp06:role-assignment/jp06:url

或最短形式:

//jp06:role-assignment/jp06:url

编辑:您可以尝试在本地执行此操作(在 builder.parse 之后设置 your.xml 的路径)吗?

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);

DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("pathtoyour.xml");

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//jp06:role-assignment/jp06:url";
NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);

System.out.println(nodes);

关于java - XML字符串解析Document应该有更大的值时却是0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com