我有一个XML格式存储在String变量中。就是这样
String xml =“ myXML”;
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
在上面的XML中,您可以看到有多个ITEMS。有人可以使用循环或其他方式告诉我如何将每个项目存储在arrayList或其他内容中吗?如果可以尝试,我会很乐意,但是很遗憾,我不知道该怎么做。
是否知道有人可以提供帮助?
请注意,XML作为字符串存储在字符串变量中。
答案 0 :(得分:0)
您可以使用Java轻松解析XML。
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import java.io.StringReader;
//...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(YOUR_XML_STRING));
Document xml = builder.parse(is);
Element root = xml.getDocumentElement();
Node node = root.getFirstChild();
while(node != null) {
//Browse nodes
node = node.getNextSibling();
}
} catch (ParserConfigurationException | SAXException | IOException e) {
Logger.getGlobal().log(Level.SEVERE, "Couldn't load config", e);
}
请注意,您还可以按ID或标签名进行搜索,例如:
xml.getElementsByTagName("item"); //Returns a NodeList
阅读the doc了解更多信息。
答案 1 :(得分:0)
您可以创建类似以下代码的项的ArrayList:
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class Lab {
public static Map<String,List<String>> hMap = new LinkedHashMap<>();
public static void main(String r[]) {
// your xml string
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"\r\n" +
"<shiporder orderid=\"889923\"\r\n" +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" +
"xsi:noNamespaceSchemaLocation=\"shiporder.xsd\">\r\n" +
" <orderperson>John Smith</orderperson>\r\n" +
" <shipto>\r\n" +
" <name>Ola Nordmann</name>\r\n" +
" <address>Langgt 23</address>\r\n" +
" <city>4000 Stavanger</city>\r\n" +
" <country>Norway</country>\r\n" +
" </shipto>\r\n" +
" <item>\r\n" +
" <title>Empire Burlesque</title>\r\n" +
" <note>Special Edition</note>\r\n" +
" <quantity>1</quantity>\r\n" +
" <price>10.90</price>\r\n" +
" </item>\r\n" +
" <item>\r\n" +
" <title>Hide your heart</title>\r\n" +
" <quantity>1</quantity>\r\n" +
" <price>9.90</price>\r\n" +
" </item>\r\n" +
"</shiporder>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(s));
Document doc = builder.parse(is);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
for (int parameter = 0; parameter < nodeList.getLength(); parameter++) {
List<String> items = new ArrayList<>();
Node node = nodeList.item(parameter);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
String title = eElement.getElementsByTagName("title").item(0).getTextContent();
String quantity = eElement.getElementsByTagName("quantity").item(0).getTextContent();
String price = eElement.getElementsByTagName("price").item(0).getTextContent();
items.add(title);
items.add(quantity);
items.add(price);
hMap.put("item" + parameter, items);
}
}
} catch (Exception e) {
e.printStackTrace();
}
hMap.forEach((h,k) -> {
System.out.println(h + ":" + k); // you can get values like hMap.get("item0") and next hMap.get("item1") etc.
});
}
}
希望对您有帮助。 我只收集了项目的共同要素。 希望对您有帮助:)