У меня есть вопрос, чтобы найти элементы XML с использованием XPATH и JAVA, в которых есть некоторые ограничения.Дайте мне знать, если вам нужна дополнительная информация, или если этот вопрос задан, пожалуйста, предоставьте ссылку, которая поможет узнать больше.
Любые указатели на то, что нужно изменить, могут быть полезны
Ниже приведен XML-код, который у меня есть на данный момент.
Из приведенного ниже XML я хочу получить только текст для <Name>
, <LocalAddress>
и <Zipcode>
для <Name = 123 School>
, а затем создать HashMap, где <Name>
будет ключом, а значения - Join.из <LocalAddress and ZipCode>
На данный момент я могу получить весь список Name, LocalAddress и zipcode, но не для определенного элемента <Name>
.
<SchoolRoot>
<School>
<Name>123 School</Name>
<Address>
<LocalAddress>
<Street>This a fixed value ( In this example, lets say ) 123 St </Street>
<AptNo>This is user input Value ( Can be anything )</AptNo>
</LocalAddress>
<LocalAddress>
<Street>This a fixed value ( In this example, lets say ) 345 St </Street>
<AptNo>This is user input Value ( Can be anything )</AptNo>
</LocalAddress>
<ZipCode>123</ZipCode>
</Address>
</School>
<School>
<Name>34564 School</Name>
<Address>
<LocalAddress>
<Street>This a fixed value ( In this example, lets say ) 678 St </Street>
<AptNo>This is user input Value ( Can be anything )</AptNo>
</LocalAddress>
<LocalAddress>
<Street>This a fixed value ( In this example, lets say ) 91011 St </Street>
<AptNo>This is user input Value ( Can be anything )</AptNo>
</LocalAddress>
<ZipCode>121314</ZipCode>
</Address>
</School>
</SchoolRoot>
Пожалуйста, проверьте приведенный ниже код, который я пробовал
package com.gami.leetcode;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;
public class xPathExample
{
public static void main(String[] args) throws Exception
{
//Build DOM
String xml = "<SchoolRoot>\n" +
"<School>\n" +
" <Name>123 School</Name>\n" +
" <Address>\n" +
" <LocalAddress>\n" +
" <Street>This a fixed value ( In this example, lets say ) 123 St </Street>\n" +
" <AptNo>This is user input Value ( Can be anything )</AptNo>\n" +
" </LocalAddress>\n" +
" <LocalAddress>\n" +
" <Street>This a fixed value ( In this example, lets say ) 345 St </Street>\n" +
" <AptNo>This is user input Value ( Can be anything )</AptNo>\n" +
" </LocalAddress>\n" +
" <ZipCode>123</ZipCode>\n" +
" </Address>\n" +
"</School>\n" +
"<School>\n" +
" <Name>456 School</Name>\n" +
" <Address>\n" +
" <LocalAddress>\n" +
" <Street>This a fixed value ( In this example, lets say ) 678 St </Street>\n" +
" <AptNo>This is user input Value ( Can be anything )</AptNo>\n" +
" </LocalAddress>\n" +
" <LocalAddress>\n" +
" <Street>This a fixed value ( In this example, lets say ) 91011 St </Street>\n" +
" <AptNo>This is user input Value ( Can be anything )</AptNo>\n" +
" </LocalAddress>\n" +
" <ZipCode>121314</ZipCode>\n" +
" </Address>\n" +
"</School>\n" +
"</SchoolRoot>";
InputSource source = new InputSource(new StringReader(xml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(source);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expr = xpath.compile("//School/Name|//Address/LocalAddress/*");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getFirstChild().getNodeValue());
}
}
}
Вывод будет выглядеть примерно так:
123 School
This a fixed value ( In this example, lets say ) 123 St
This is user input Value ( Can be anything )
This a fixed value ( In this example, lets say ) 345 St
This is user input Value ( Can be anything )
456 School
This a fixed value ( In this example, lets say ) 678 St
This is user input Value ( Can be anything )
This a fixed value ( In this example, lets say ) 91011 St
This is user input Value ( Can be anything )
, но ожидаемый результат будет иметь одну школудетали за один раз, включая почтовый индекс.Как то так
123 School
This a fixed value ( In this example, lets say ) 123 St
This is user input Value ( Can be anything )
This a fixed value ( In this example, lets say ) 345 St
This is user input Value ( Can be anything ) and 'Zipcode' : <ZipCode>