Thanks for considering this question.
I'm reading a complex XML file, which as you can see in the code has 44 main "nodes". Each node has further nested elements and so on.
I have managed to read the info from the first node but it seems that after the first iteration, returns only null. What could I be missing?
for (int i=0; i<nodeList.getLength(); i++){
log(String.valueOf(i));
Element flightInfo = (Element)nodeList.item(i);
NodeList flights = flightInfo.getElementsByTagName("flight");
Element flight = (Element)flights.item(0);
String flightId = flight.getAttribute("id");
String airlineCode = flight.getAttribute("airlineCode");
String operationType = flight.getAttribute("operationType");
String flightRoute= flight.getAttribute("flightType");
String scheduledTime = flight.getAttribute("scheduledTime");
NodeList routingList = flight.getElementsByTagName("routingList");
Element iatas = (Element)routingList.item(0);
NodeList _iata = (iatas.getElementsByTagName("IATA"));
String iata = _iata.item(i).getFirstChild().getNodeValue();
NodeList times = flight.getElementsByTagName("times");
Element realTimes = (Element)times.item(0);
NodeList _realTime = (realTimes.getElementsByTagName("realTime"));
String realTime = _realTime.item(0).getFirstChild().getNodeValue();
NodeList means = flight.getElementsByTagName("means");
Element gates = (Element)means.item(0);
NodeList _gate = gates.getElementsByTagName("gate");
Element gate = (Element)_gate.item(0);
String gateId = gate.getAttribute("id");
Element bagClaimList = (Element)means.item(0);
NodeList bagClaims = bagClaimList.getElementsByTagName("bagClaim");
Element bagClaim = (Element)bagClaims.item(0);
String bagId = bagClaim.getAttribute("id");
Element standList = (Element)means.item(0);
NodeList stands = standList.getElementsByTagName("stand");
Element _stand = (Element)stands.item(i);
String standId = _stand.getAttribute("id");
NodeList remarks = flight.getElementsByTagName("flight");
Element remarkCodes = (Element)remarks.item(0);
NodeList _remarkCode = (remarkCodes.getElementsByTagName("remarkCode"));
String remarkCode = _remarkCode.item(0).getFirstChild().getNodeValue();
flightList.add(new Flight(flightId, airlineCode, operationType,iata, scheduledTime, iata, realTime, gateId, bagId, standId, remarkCode));
log("Added new flightInfo");
}
The XML I'm reading is the following:
<flightData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://c:/SITA/IKUSI FIDS/FIDS.XSD">
<flightInfo>
<flight id="AM2613" airlineCode="AM" flightNumber="2613" operationType="D" flightType="D" scheduledTime="2013-07-18T07:00:00">
<routingList>
<IATA>MTY</IATA>
</routingList>
<times>
<realTime>2013-07-18T07:00:00</realTime>
</times>
<means>
<gateList>
<gate id="N/14"/>
</gateList>
<bagClaimList>
<bagClaim id="2"/>
</bagClaimList>
<standList>
<stand id="5"/>
</standList>
</means>
<remarks>
<remarkCode>DEP</remarkCode>
</remarks>
</flight>
</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>
You'd be better off using JAXB: with the xsd file you will be able to generate java classes representing your model and won't have to write all this data extraction code.
Related
I have to extract tag value from an xml Document that contains a single tag like below:
<error>Permission denied</error>
i have tried:
String xmlRecords = "<error>Permission denied</error>"
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
Node nodes = doc.getFirstChild();
String = nodes.getNodeValue();
but it dont works.
How can i do it ?
Use doc.getDocumentElement().getTextContent() to get the string Permission denied.
With DOM it´s util to know the structure of the XML document, and which node level are you looking for.
After get Document, you can use document.getElementsByTagName("root") to look for the root or father tags, and get the childs as a list to look for the item. Something like this:
NodeList listresults = document.getElementsByTagName('father/root element string');
NodeList nl = listresults.item(0).getChildNodes();
// Recorremos los nodos
for (int temp = 0; temp < nl.getLength(); temp++) {
Node node = nl.item(temp);
// Check if it is a node
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(element.getNodeName().equals("error")){
// check the element
}
}
}
I hope this helps you.
just try following code.
String value = nodes.getTextContent();
You have to construct the string if you are using the above approach. You will get the string values of the tag name and content using the functions.
Tag name = nodes.getTextContent()
tag value = nodes.getLocalName()
I guess this is what you want
Element element = document.getDocumentElement();
NodeList errorTagList = element.getElementsByTagName("error");
if (errorTagList != null && errorTagList.getLength() > 0) {
NodeList errorTagSubList = errorTagList.item(0).getChildNodes();
if (errorTagSubList != null && errorTagSubList.getLength() > 0) {
String value = errorTagSubList.item(0).getNodeValue();
}
}
I try to examine with Java XPath an html string like this:
<app>
<elem class="A">value1</elem>
<elem class="B">value2a<br />value2b</elem>
<elem class="C">value3</elem>
</app>
Actually for obtain the elem's value i use this code
public String getValue(String xml, String classValue){
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource source = new InputSource(new StringReader(xml));
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = db.parse(source);
String xpathRequest = "//*[#class='"+classValue+"']/text()";
String value = xpath.evaluate(xpathRequest , document);
return value;
}
For classes A and C works fine, but when i ask the content of task with class B obtain only value2a
How i can get the complete string of node?
Simply run
String xpathRequest = "//*[#class='"+class+"']";
String value = this.xpath.evaluate(xpathRequest , document);
This will select the <elem> node and when converted to a String build the concatenation of all text content, e.g. Value2a Value2b
To get a list of all text contents below a Elem you need to select them as NodeSet:
String xpathRequest = "//*[#class='"+class+"']/text()";
NodeList textNodes = (NodeList)xpath.evaluate(xpathRequest , document, XPathConstants.NODESET);
ArrayList<String> texts = new ArrayList<>();
for (int i=0; i<textNodes.getLength(); i++)
texts.add(textNodes.item(i).getTextContent());
It is because xpath will return 2 value at this moment. Try below :-
List<WebElement> allprice = driver.findElements(By.xpath("//*[#class='B']/text()"));
for(WebElement a:WebElement allprice){
System.out.println(a.gettext());
}
How do you get xPath value for more than one path in a single call.
for example
<Message version="010" release="006" xmlns="http://www.ncpdp.org/schema/SCRIPT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
</Header>
<Body>
<CommunicationNumbers>
<Communication>
<Number>5551234444</Number>
<Qualifier>TE</Qualifier>
</Communication>
<Communication>
<Number>5551235555</Number>
<Qualifier>FX</Qualifier>
</Communication>
</CommunicationNumbers>
<Identification>
<FileID>616</FileID>
<DEANumber>AB123456</DEANumber>
<SocialSecurity>123456789</SocialSecurity>
</Identification>
<Specialty>A</Specialty>
<ClinicName>Therapy Department</ClinicName>
<Name>
<LastName>Xavior</LastName>
<FirstName>Charles</FirstName>
<MiddleName>C</MiddleName>
<Suffix>MD</Suffix>
</Name>
<Address>
<AddressLine1>888 ABC Drive</AddressLine1>
<AddressLine2>Suite 200</AddressLine2>
<City>Miami</City>
<State>FL</State>
<ZipCode>12345</ZipCode>
</Address>
</Body>
and I need values for :
:Communication/Number
:Identification/FileID
:Specialty
in a single call.
For a single value I am using
public static String getExpValue(final String xmlString, final String expression, final ServiceNamespaceContext nameSpace) throws XPathExpressionException {
StringReader strReader = new StringReader(xmlString);
InputSource inputStr = new InputSource(strReader);
String result = null;
try {
final XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(nameSpace);
final XPathExpression expr = xpath.compile(expression);
result = (String) expr.evaluate(inputStr, XPathConstants.STRING);
} finally {
strReader = null;
inputStr = null;
}
return result;
}
My desired output is a single concatenated String 5551234444616A
You could try using something like...
XPathExpression expr = xpath.compile("//Communication/Number | //Identification/FileID");
Which should combine the results of each query. In my (simply) test, I got 3 matches (2 for Communication/Number and 1 for Identification/FileID)
Updated
The intended result was to return a NodeList, for example...
NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODELIST);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String value = node.getTextContent();
System.out.println(value);
}
Since Java 7 the NodeList is replaced by NodeSet:
NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String value = node.getTextContent();
System.out.println(value);
}
I have a function I would like to loop through the xml and pull out certain tags.
My xml looks like this:
<Report_Data>
<Report_Entry>
<Company>Test</Company>
<Name>Test Name</Name>
<Division>Test Division</Division>
</Report_Entry>
<Report_Entry>
<Company>Test 2</Company>
<Name>Test Name 2</Name>
<Division>Test Division 2</Division>
</Report_Entry>
<Report_Entry>
<Company>Test 3</Company>
<Name>Test Name 3</Name>
<Division>Test Division 3</Division>
</Report_Entry>
</Report_Data>
Here is my code to loop through:
String comp, name, div, nodeName, NodeValue;
Node node;
try
{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(coaFULL));
Document doc2 = (Document) xpath.evaluate("/", source, XPathConstants.NODE);
NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);
System.out.println("NODE LIST LENGTH =" + nodeList.getLength());
String nodeName, nodeValue = "";
Node node;
for(int i = 0; i < nodeList.getLength(); i++)
{
node = nodeList.item(i);
node = nodeList.item(i).getFirstChild();
nodeName = node.getNodeName();
nodeValue = node.getChildNodes().item( 0 ).getNodeValue();
if(nodeName.equals("Company"))
{
comp = nodeValue;
}
else if( nodeName.equals("Name"))
{
name = nodeValue;
}
else if(nodeName.equals("Division"))
{
div = nodeValue;
}
System.out.println("COMPANY = " + comp);
System.out.println("NAME = " + name);
System.out.println("DIVISION = " + div);
}
When I run my code, only the first value (company) gets an actual value, everything else is blank. I also tried adding node = nodeList.item(i).getNextSibling(); inside of each if statement to grab the next node, but that did not work.
My nodeList does have items in it, over 1000. Is there a problem with this statement: NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry").evaluate(doc2, XPathConstants.NODESET);?
Should it be: NodeList nodeList = (NodeList) xpath.compile("/Report_Data/Report_Entry/*").evaluate(doc2, XPathConstants.NODESET);
I tried it with the /* at the end but that caused the nodeList to have every single node in it. I want to make sure that when I grab a Report_Entry node, that I set the string variables to the correct values that correspond to each other.
==========================================================
Solution: It's ugly but my solution was to just go with one loop and use the second list of children nodes with hard coded values:
for(int i = 0; i < nodeList.getLength(); i++)
{
node = nodeList.item(i);
tempList = node.getChildNodes();
System.out.println("TEMP LIST LENGTH =" + tempList.getLength());
comp = tempList.item(0).getTextContent();
name = tempList.item(1).getTextContent();
div = tempList.item(2).getTextContent();
}
Thanks to #hage for his help.
Maybe it's because your node is only the first child?
node = nodeList.item(i);
node = nodeList.item(i).getFirstChild();
I guess nodeList.item(i) will give you the Report_Entrys and their first child is the Company.
You will need to loop over all children of the Company entry
EDIT (regarding your edit):
tempList.item(x) is the Company, Name, and then Division. When you get the first child of this one, you are at the text node (the actual content). And because you try to get the name of this node, you get the #text output (see this).
To get name and value of the nodes, try this (untested)
nodeName = tempList.item(x).getNodeName();
nodeValue = tempList.item(x).getTextContent();
I am using DOM to parse an XML string as in the following example. This works great except in one instance. The document which I am trying to parse looks like this:
<response requestID=\"1234\">
<expectedValue>Alarm</expectedValue>
<recommendations>For steps on how to resolve visit Website and use the search features for \"Alarm\"<recommendations>
<setting>Active</setting>
<response>
The code I used to parse the XML is as follows:
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlResult));
Document doc = db.parse(is);
NodeList nlResponse = doc.getElementsByTagName("response");
String[] String = new String[3]; //result entries
for (int i = 0; i < nlResponse.getLength(); i++) {
Element e = (Element) nlResponse.item(i);
int c1 = 0; //count for string array
NodeList ev = e.getElementsByTagName("expectedValue");
Element line = (Element) ev.item(0);
String[c1] = (getCharacterDataFromElement(line));
c1++;
NodeList rec = e.getElementsByTagName("recommendations");
line = (Element) rec.item(0);
String[c1] = (getCharacterDataFromElement(line));
c1++;
NodeList set = e.getElementsByTagName("settings");
line = (Element) set.item(0);
String[c1] = (getCharacterDataFromElement(line));
c1++;
I am able to parse the code and put the result into a string array (as opposed to the System.out.println()). With the current code, my string array looks as follows:
String[0] = "Alarm"
String[1] = "For steps on how to resolve visit"
String[2] = "Active"
I would like some way of being able to read the rest of the information within "Recommendations" in order to ultimately display the hyperlink (along with other output) in a TextView. How can I do this?
I apologize for my previous answer in assuming your xml was ill-formed.
I think what is happening is that your call to the getCharacterDataFromElement is only looking at the first child node for text, when it will need to look at all the child nodes and getting the href attribute as well as the text for the 2nd child node when looking at the recommendations node.
e.g. after getting the Element for recommendation
String srec = "";
NodeList nl = line.getChildNodes();
srec += nl.item(0).getTextContent();
Node n = nl.item(1);
NamedNodeMap nm = n.getAttributes();
srec += "" + n.getTextContent() + "";
srec += nl.item(2).getTextContent();
String[c1] = srec;