Parser to parse unknown XML Schema in java -
I tried to understand all other answers in StackVerflow. But I am not able to address those answers to my question.
When I call a web service, I get the feedback I get the schema from response.getData () (The result of the XML in the data table is the result. ) (return type string) . We do not know what data we get in that XML.
I need to use a third party parser, so that when I give the above string in one method in that parser, then it should return all the elements in that XML and then I need the necessary elements I can print.
I do not want to start parsing the XML myself, if there is any way, can it be from me? (Does this also mean any?) Sorry, if I'm completely wrong (edit axis2 / eclipse ) (edited)
Edit: I already Trying to add code which is to add.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance (); Node list nodelist = null; Try {string xml = res2.getResult (). GetRawData (); DocumentBuilder db = dbf.newDocumentBuilder (); Document document = db Pars (new bytereinputstream (xml.getBytes ())); Nodelist = document.getElementsByTagName ("phone number"); NamedNodeMap attrib = document.getAttributes (); For (int i = 0; i & lt; attrib.getLength (); i ++) {string node name = attrib.item (i) .getNodeName (); // nodename string node = attrib.item (i) .getNodeValue (); } But I'm not sure that PhoneNumber is with that tag or other name. Also, we do not know how many tags we have.
Thank you, using the code by SyamS, I am able to print the values related to all nodes and XML. Now I want to store node name and node values in that list as a hashp along with the key.
Example XML:
& lt; Docs & gt; & Lt; Doctor & gt; & Lt; Eid & gt; 12 & lt; / Id & gt; & Lt; Phone & gt; 1234 & lt; / Phone & gt; & Lt; / Doctor & gt; & Lt; Doctor & gt; & Lt; Eid & gt; 147 & lt; / Id & gt; & Lt; Phone & gt; 12345 & lt; / Phone & gt; & Lt; Closed & gt; False & lt; / Off & gt; & Lt; Bid & gt; 2 & lt; / B & gt; & Lt; Postal ID & gt; 8 & lt; / Postal ID & gt; & Lt; Date & gt; 2014-02-04T12: 18: 50.063-07: 00 & lt; / Date & gt; & Lt; Instant & gt; False & lt; / Immediate & gt; & Lt; / Doctor & gt; & Lt; / Docs & gt;
For this, you do not need a third-party library. You can simply identify all the leaf nodes using xpath and read the value (as well as the attributes). For example
Public Stable Maps & lt; String, list & lt; String & gt; & Gt; ParseXml (string xml) throws XMLStreamException {StringBuilder content = null; Maps & lt; String, list & lt; String & gt; & Gt; Datamap = new hashmap & lt; & Gt; (); XMLInputFactory Factory = XMLInputFactory.newInstance (); InputStream stream = new bytereinputstream (xml.getBytes ()); XMLStreamReader Reader = Factory .createXMLStreamReader (stream); While (reader.hasNext ()) {int event = reader.next (); Switch (event) {case XMLStreamConstants.EELEMENT: content = new StringBuilder (); break; Case XMLStreamConstants.CARACTERS: if (content! = Zero) {content.append (reader.getText (). Trim ()); } break; Case XMLStreamConstants.END_ELEMENT: If (content! = Null) {string-leafText = content.toString (); If (dataMap.get (reader.getLocalName ()) == tap) {List & lt; String & gt; Value = new ArrayLight & lt; & Gt; (); Values.add (leafText); DataMap.put (reader.getLocalName (), value); } Other {dataMap.get (reader.getLocalName ()). Add (leafText); }} Content = null; break; Case XMLStreamConstants.START_DOCUMENT: Break; }} Return DataMap; }
Comments
Post a Comment