XML文件
<?xml version="1.0" encoding="UTF-8"?>
<bins>
<bin>
<number>100510</number>
<brand>LOCAL CARD</brand>
<bank></bank>
<type></type>
<level></level>
<isocountry>UNITED KINGDOM</isocountry>
<info></info>
<country_iso>GB</country_iso>
<country2_iso>GBR</country2_iso>
<country3_iso>826</country3_iso>
<www>NULL</www>
<phone>NULL</phone>
<former_bank>NULL</former_bank>
<address>NULL</address>
</bin>
<bin>
<number>100515</number>
<brand>LOCAL CARD</brand>
<bank></bank>
<type></type>
<level></level>
<isocountry>NORWAY</isocountry>
<info></info>
<country_iso>NO</country_iso>
<country2_iso>NOR</country2_iso>
<country3_iso>578</country3_iso>
<www>NULL</www>
<phone>NULL</phone>
<former_bank>NULL</former_bank>
<address>NULL</address>
</bin>
<bin>
<number>103753</number>
<brand>LOCAL CARD</brand>
<bank>I&M BANK</bank>
<type>DEBIT</type>
<level>PREPAID</level>
<isocountry>KENYA</isocountry>
<info></info>
<country_iso>KE</country_iso>
<country2_iso>KEN</country2_iso>
<country3_iso>404</country3_iso>
<www></www>
<phone></phone>
<former_bank>NULL</former_bank>
<address>NULL</address>
</bin>
<bin>
<number>104001</number>
<brand>LOCAL CARD</brand>
<bank>LUXURY JEWELLERY CLASS (LJC) - TDFS</bank>
<type></type>
<level></level>
<isocountry>CANADA</isocountry>
<info></info>
<country_iso>CA</country_iso>
<country2_iso>CAN</country2_iso>
<country3_iso>124</country3_iso>
<www></www>
<phone></phone>
<former_bank></former_bank>
<address></address>
</bin>
</bins>
代码
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
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.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Component
public abstract class XMLDataParser extends FileParser {
public static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
List<String> binList = new ArrayList<>();
List<List<String>> binDataList = new ArrayList<>();
@Override
public List<List<String>> parseFile(String fileContent) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException {
InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression1 = xpath.compile("//bins/*");
Object result1 = xPathExpression1.evaluate(document, XPathConstants.NODESET);
XPathExpression xPathExpression2 = xpath.compile("//bins//bin//*/text()");
Object result2 = xPathExpression2.evaluate(document, XPathConstants.NODESET);
NodeList nodeList1 = (NodeList) result1;
NodeList nodeList2 = (NodeList) result2;
for (int i = 0; i < nodeList1.getLength(); i++) {
for (int j = 0; j < nodeList2.getLength(); j++) {
binList.add(nodeList2.item(j).getNodeValue());
}
binDataList.add(binList);
}
System.out.println(binList);
return binDataList;
}
}
输出
[[100510,LOCAL CARD,UNITED KINGDOM,GB,GBR,826,NULL,NULL,NULL, NULL,100515,LOCAL CARD,NORWAY,NO,NOR,578,NULL,NULL,NULL, NULL,103753,LOCAL CARD,I&amp; M BANK,DEBIT,PREPAID,KENYA,KE,KEN, 404,NULL,NULL,104001,LOCAL CARD,LUXURY JEWELLERY CLASS(LJC) - TDFS,CANADA,CA,CAN,124,100510,LOCAL CARD,UNITED KINGDOM,GB, GBR,826,NULL,NULL,NULL,NULL,100515,LOCAL CARD,NORWAY,NO,NOR, 578,NULL,NULL,NULL,NULL,103753,LOCAL CARD,I&amp; M BANK,DEBIT, PREPAID,KENYA,KE,KEN,404,NULL,NULL,104001,LOCAL CARD,LUXURY JEWELLERY CLASS(LJC) - TDFS,CANADA,CA,CAN,124,100510,LOCAL CARD,UNITED KINGDOM,GB,GBR,826,NULL,NULL,NULL,NULL,100515, LOCAL CARD,NORWAY,NO,NOR,578,NULL,NULL,NULL,NULL,103753, LOCAL CARD,I&amp; M BANK,DEBIT,PREPAID,KENYA,KE,KEN,404,NULL,NULL, 104001,LOCAL CARD,LUXURY JEWELLERY CLASS(LJC) - TDFS,CANADA,CA, CAN,124,100510,LOCAL CARD,UNITED KINGDOM,GB,GBR,826,NULL, NULL,NULL,NULL,100515,LOCAL CARD,NORWAY,NO,NOR,578,NULL, NULL,NULL,NULL,103753,LOCAL CARD,I&amp; M BANK,DEBIT,PREPAID,KENYA, KE,KEN,404,NULL,NULL,104001,LOCAL CARD,LUXURY JEWELLERY CLASS (LJC) - TDFS,CANADA,CA,CAN,124]]
我需要输出
[[100510,LOCAL CARD,UNITED KINGDOM,GB,GBR,826,NULL,NULL,NULL, NULL],[100515,LOCAL CARD,NORWAY,NO,NOR,578,NULL,NULL,NULL, NULL],[103753,LOCAL CARD,I&amp; M BANK,DEBIT,PREPAID,KENYA,KE,KEN, 404,NULL,NULL],[104001,LOCAL CARD,LUXURY JEWELLERY CLASS(LJC) - TDFS,CANADA,CA,CAN,124]]
但是,我无法得到这个。请提供解决方案。 我必须使用Xpath来获得此输出。
答案 0 :(得分:0)
看起来您希望第二个XPath与第一个XPath返回的// Any tablet device.
if( $detect->isTablet()) {
}
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet()) {
}
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS()) {
}
if( $detect->isAndroidOS()) {
}
if( $detect->isWindowsPhoneOS()) {
}
相关。此外,对于每个$detect = new Mobile_Detect;
if($detect->isMobile() || $detect->isTablet()) {
echo "<link rel='stylesheet' href='mobile.css type='text/css' />";
} else {
echo "<link rel='stylesheet' href='style.css type='text/css' />";
}
,您需要一个不同的bin
实例,因此需要在外部循环的范围内声明ArrayList
。我不是Java开发人员,但我认为它应该是这样的:
bin