xpath 쿼리가 작동하지 않아 몇 시간 동안 솔루션을 찾을 수 없습니다. 모든 도움에 감사드립니다. 표현을위한 이전의 시도xpath 쿼리 결과가 없습니다.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource inputSource = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")));
Document xmldocument = db.parse(inputSource);
XPath xPath = XPathFactory.newInstance().newXPath();
String prefix = xmldocument.getDocumentElement().getPrefix();
searchElementsXml = new String[]{"name", "id", "version", "description", "keywords", "authorInfos.name", "status"};
for(int j = 0; j < searchElementsXml.length; j++){
String expression ="/*/" + prefix + ":" + searchElementsXml[j];
NodeList nodeList = (NodeList)
xPath.compile(expression).evaluate(xmldocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
}
부 :
String expression ="/*/" + prefix + ":" + searchElementsXml[j];
String expression ="/*:serviceSpecification" "/*:" + searchElementsXml[j];
String expression ="/*/*:id"
그들은 https://www.freeformatter.com/xpath-tester.html#ad-output에서 작동하지만 내 자바 코드이다. 나는 다음과 같은 라인을 aded 한
<?xml version="1.0" encoding="UTF-8"?>
<ServiceSpecificationSchema:serviceSpecification xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ServiceSpecificationSchema="http://foo.bar/ServiceSpecificationSchema.xsd" xsi:schemaLocation="http://foo.barServiceSpecificationSchema.xsd ServiceSpecificationSchema.xml">
<ServiceSpecificationSchema:id>someid</ServiceSpecificationSchema:id>
<ServiceSpecificationSchema:name>myname</ServiceSpecificationSchema:name>
....
예를 들어 버전에 의해 닫힌 xml - name 요소에 오류가 있습니다. – Michal
답변 해 주셔서 감사합니다. 파트를 복사하는 동안 오류가 발생했으며 원래 코드에는 없습니다. 나는 그것을 조정하고있다. 다른 해결책이 있습니까? – Simon