我尝试了解stackoverflow中的所有其他答案。但我无法将这些答案与我的问题联系起来。
当我致电网络服务时,我得到response
。我按response.getData();(The XML of the data table containing the results.)
(return type String)
获取了架构。我们不知道我们在XML中获得了什么数据。
我需要使用第三方解析器,因此当我将上述字符串提供给该解析器中的一个方法时,它应该返回该XML中的所有元素,然后我可以打印所需的元素。
我不想自己开始解析XML。有没有办法可以做到这一点? (它甚至有意义吗?)。对不起如果我完全错了。 (使用Axis2/eclipse
)(已编辑)
编辑:添加我已尝试过的代码。
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
NodeList nodeList = null;
try {
String xml = res2.getResult().getRawData();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new ByteArrayInputStream(xml.getBytes()));
nodeList = document.getElementsByTagName("PhoneNumber");
NamedNodeMap attrib = document.getAttributes();
for (int i = 0; i < attrib.getLength(); i++) {
String nodeName = attrib.item(i).getNodeName();
//nodeName
String nodeValue = attrib.item(i).getNodeValue();
}
但我不确定PhoneNumber是否带有该标签或其他名称。我们也不知道我们有多少标签。
谢谢,使用SyamS的代码,我能够从xml打印所有节点和相应的值。现在我想将它存储到一个hashmap中,其中key作为节点名称和列表中的节点值。
示例XML:
<Docs>
<Doc>
<Id>12</Id>
<Phone>1234</Phone>
</Doc>
<Doc>
<Id>147</Id>
<Phone>12345</Phone>
<Locked>false</Locked>
<BID>2</BID>
<DocId>8</DocId>
<Date>2014-02-04T12:18:50.063-07:00</Date>
<Urgent>false</Urgent>
</Doc>
</Docs>
答案 0 :(得分:1)
您无需为此寻找第三方库。您可以使用xpath简单地识别所有叶节点并读取值(以及属性)。例如
public static Map<String, List<String>> parseXml(String xml) throws XMLStreamException {
StringBuilder content = null;
Map<String, List<String>> dataMap = new HashMap<>();
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream stream = new ByteArrayInputStream(xml.getBytes());
XMLStreamReader reader = factory.createXMLStreamReader(stream);
while (reader.hasNext()) {
int event = reader.next();
switch (event) {
case XMLStreamConstants.START_ELEMENT:
content = new StringBuilder();
break;
case XMLStreamConstants.CHARACTERS:
if (content != null) {
content.append(reader.getText().trim());
}
break;
case XMLStreamConstants.END_ELEMENT:
if (content != null) {
String leafText = content.toString();
if(dataMap.get(reader.getLocalName()) == null){
List<String> values = new ArrayList<>();
values.add(leafText);
dataMap.put(reader.getLocalName(), values);
} else {
dataMap.get(reader.getLocalName()).add(leafText);
}
}
content = null;
break;
case XMLStreamConstants.START_DOCUMENT:
break;
}
}
return dataMap;
}
答案 1 :(得分:0)
您应该阅读与Best XML parser for Java相关的答案。使用我已下载到我的C:驱动器上的临时文件夹的Sample XML File (books.xml)中的示例XML,您可以使用Java的本机SAXParser库。这是一个可用于迭代XML中所有元素的示例类。在项目中创建类,并将其解析方法称为:
File xml = new File("c:/temp/books.xml");
MySaxParser sax = new MySaxParser(xml);
sax.parseXml();
这是您可以复制到项目中以进行尝试的课程。当然,根据您的需要进行修改。导入应该引导您到适当的Java API页面,例如Class SAXParser开始。
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MySaxParser extends DefaultHandler {
private String absolutePathToXml = "";
public MySaxParser(File xml) {
absolutePathToXml = xml.getAbsolutePath();
}
/**
* Parses an XML file into memory
*/
public void parseXml() {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(absolutePathToXml, this);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException: ");
e.printStackTrace();
} catch (SAXException e) {
System.out.println("SAXException: ");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException: ");
e.printStackTrace();
}
}
/**
* Event: Parser starts reading an element
*/
@Override
public void startElement(String s1, String s2, String elementName, Attributes attributes)
throws SAXException {
//print an element's name
System.out.println("element: " + elementName);
//print all attributes for this element
for(int i = 0; i < attributes.getLength(); i++) {
System.out.println("attribute: " + attributes.getValue(i));
}
}
}
答案 2 :(得分:-1)
这段代码无可挑剔我已经做了很多研究将适用于任何xml我已经使用过的DOM Parser只是给出了XML Path的位置它会读取所有的XML标签
示例XML:
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size>
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size>
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
JAVA代码:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xmlTraversingRevision.xmlTraversal;
import java.util.*;
public class domParser {
static HashMap<String,HashMap<String,String>> finalResult = new HashMap<String,HashMap<String,String>>();
static HashMap<String,HashMap<String,String>> requiredResult = new HashMap<String,HashMap<String,String>>();
static HashMap<String,String> map=new HashMap<>();
static HashMap<String,String> map1=new HashMap<>();
static ArrayList<String> list = new ArrayList<>();
static ArrayList<String> list1=new ArrayList<>();
public void readXmlAttributes() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException{
{
//HashMap<String,HashMap<String,String>> requiredResult = new HashMap<String,HashMap<String,String>>();
/**
* DocumentBuilderFactory.newInstance() return instance of XMLDom parser
* We can also specify the ClassName directly for XMLParser in newInstance method like
* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
* ("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",null)
*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("Enter XML Path Location"));
doc.normalizeDocument();
Element root=doc.getDocumentElement();
// System.out.println ("Root element of the doc is :" + doc.getDocumentElement().getNodeName());
/**
* Getting Particular node details
*/
if (doc.hasChildNodes()) {
/**
* Getting all child node from XMLDocument using
* doc.getDocumentElement().getChildNodes() Or doc.getChildNodes()
*/
//getAllNodeNAttribute(doc.getDocumentElement().getChildNodes());
//requiredResult=
getAllNodeNAttribute(doc.getChildNodes(),doc);
System.out.println("********:"+requiredResult);
}
// return requiredResult;
}
}
public static void getAllNodeNAttribute(NodeList nodes,Document doc) throws XPathExpressionException {
// HashMap<String,HashMap<String,String>> finalResult = new HashMap<String,HashMap<String,String>>();
String rootElement=doc.getDocumentElement().getTagName();
String rootNextChild=doc.getDocumentElement().getChildNodes().item(1).getNodeName();
Element c=doc.getDocumentElement();
Node node = null;
Object z;
Object y;
for (int i = 0; i < nodes.getLength(); i++) {
node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("Node Start: " + node.getNodeName() + " [Start]");
if (node.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeAttrbs = node.getAttributes();
for (int j = 0; j < nodeAttrbs.getLength(); j++) {
Node attrNode = nodeAttrbs.item(j);
System.out.println("attr name: " + attrNode.getNodeName());
System.out.println("attr value: " + attrNode.getNodeValue());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
System.out.println("******Root element****** :"+rootElement);
System.out.println("*******Root Element Next Child*******:"+rootNextChild);
xmlTraversal traverse= new xmlTraversal();
try {
// finalResult= traverse.gettingAttributes(attrNode.getNodeName(),attrNode.getNodeValue() );
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xpathExp = "/"+rootElement+"/"+rootNextChild+"[@"+attrNode.getNodeName()+"=\"+"+attrNode.getNodeValue()+"\"]";
Node node1 = (Node) xpath.evaluate(xpathExp, doc, XPathConstants.NODE);
// System.out.println("AttributesValue:"+node.getTextContent());
// System.out.println(node.getNodeValue());
// System.out.println("Atrributes Name:"+node.getNodeName());
//
map.put(attrNode.getNodeValue(),node.getTextContent());
// System.out.println("Values in MAP:"+map);
if(node.getNodeType()==Node.ENTITY_NODE){
for(int r=0;r<node.getTextContent().length();r++){
// System.out.println("AtrributesName:"+node.getChildNodes().item(r));
//list.add(node.getChildNodes().item(r));
}}
}
}
if (node.hasChildNodes()) {
getAllNodeNAttribute(node.getChildNodes(),doc);
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
if(node.getNodeName()!=c.getTagName() &&node.getNodeName()!=c.getChildNodes().item(1).getNodeName()){
list.add(node.getNodeName());
}}
if (!node.hasChildNodes()) {
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
}
} else if ((nodes.getLength() <= 1)) {
System.out.println("Node Text: " + node.getNodeValue());
}
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
domParser dom= new domParser();
dom.readXmlAttributes();
}}
<强>输出:强>
Node Start: catalog [Start]
Node Start: product [Start]
attr name: description
attr value: Cardigan Sweater
******Root element****** :catalog
*******Root Element Next Child*******:product
attr name: product_image
attr value: cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: catalog_item [Start]
attr name: gender
attr value: Men's
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: item_number [Start]
Node Text: QWZ5671
Node Close: item_number [CLOSE]
Node Start: price [Start]
Node Text: 39.95
Node Close: price [CLOSE]
Node Start: size [Start]
Node Start: color_swatch [Start]
attr name: image
attr value: red_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Red
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: burgundy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Burgundy
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Start: size [Start]
attr name: description
attr value: Large
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: color_swatch [Start]
attr name: image
attr value: red_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Red
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: burgundy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Burgundy
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Close: catalog_item [CLOSE]
Node Start: catalog_item [Start]
attr name: gender
attr value: Women's
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: item_number [Start]
Node Text: RRX9856
Node Close: item_number [CLOSE]
Node Start: price [Start]
Node Text: 42.50
Node Close: price [CLOSE]
Node Start: size [Start]
Node Start: color_swatch [Start]
attr name: image
attr value: red_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Red
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: navy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Navy
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: burgundy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Burgundy
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Start: size [Start]
attr name: description
attr value: Medium
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: color_swatch [Start]
attr name: image
attr value: red_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Red
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: navy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Navy
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: burgundy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Burgundy
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: black_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Black
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Start: size [Start]
attr name: description
attr value: Large
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: color_swatch [Start]
attr name: image
attr value: navy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Navy
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: black_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Black
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Start: size [Start]
attr name: description
attr value: Extra Large
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Start: color_swatch [Start]
attr name: image
attr value: burgundy_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Burgundy
Node Close: color_swatch [CLOSE]
Node Start: color_swatch [Start]
attr name: image
attr value: black_cardigan.jpg
******Root element****** :catalog
*******Root Element Next Child*******:product
Node Text: Black
Node Close: color_swatch [CLOSE]
Node Close: size [CLOSE]
Node Close: catalog_item [CLOSE]
Node Close: product [CLOSE]
Node Close: catalog [CLOSE]