Java 搜索不同的字符串
我有下一种情况,我有一个类似xml的字符串,我想搜索这个字符串到第二个car_id 12345678并得到值 如果无法搜索车辆,请键入55555 我尝试了下面的代码,但效果不好。是否有更好的方法来执行/循环字符串?谢谢,谢谢Stackoverflow 字符串:Java 搜索不同的字符串,java,string,loops,Java,String,Loops,我有下一种情况,我有一个类似xml的字符串,我想搜索这个字符串到第二个car_id 12345678并得到值 如果无法搜索车辆,请键入55555 我尝试了下面的代码,但效果不好。是否有更好的方法来执行/循环字符串?谢谢,谢谢Stackoverflow 字符串: <car_dealer><car_id>2</car_id></car_dealer><car><car_id>12345678</car_id><
<car_dealer><car_id>2</car_id></car_dealer><car><car_id>12345678</car_id></car>
<car_dealer><car_id>2</car_id></car_dealer><car><car_type id_1="2" id_2="32">55555</car_type></car>
代码:
将完成此工作的索引: 原件: 212345678 255555 解决方案索引:
String car_id = input.substring(input.indexOf("<car><car_id>") + "<car><car_id>".length(), input.indexOf("</car_id></car>"));
对别人也一样
祝你好运 您可以使用下面的模式匹配找到汽车id。希望这对你有帮助
String xmlString = "<car_id>12345678</car_id>afhkjasd<car_id>123456789</car_id>";
Pattern pattern = Pattern.compile("(<car_id>)([0-9]{0,})(</car_id>)");
Matcher matcher = pattern.matcher(xmlString);
while (matcher.find()) {
System.out.println(matcher.group(2));
}
我强烈建议您不要使用regex或indexOf-tricky解析这些字符串。这些东西总有一天会坏的 如果看起来非常像xml的字符串实际上是xml,那么可以使用xpath解析这些值。大概是这样的:
import java.io.IOException;
import java.io.StringReader;
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.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class A {
public static void main(String[] args) throws Exception {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
String xml1 = "<xml><car_dealer><car_id>2</car_id></car_dealer><car><car_id>12345678</car_id></car></xml>";
String xml2 = "<xml><car_dealer><car_id>2</car_id></car_dealer><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>";
Document doc1 = stringToDom(xml1);
Document doc2 = stringToDom(xml2);
XPathExpression expr1 = xpath.compile("//car/car_id/text()");
String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING);
XPathExpression expr2 = xpath.compile("//car/car_type/text()");
String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING);
System.out.println("***");
System.out.println("carId: " + carId);
System.out.println("carType: " + carType);
System.out.println("***");
/* prints
***
carId: 12345678
carType: 55555
***
*/
}
public static Document stringToDom(String xmlSource) throws SAXException,
ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
}
使用XML解析API。我没有字符串。你也可以从字符串在内存中创建XML。不要开始用字符串操作解析xml,省去你自己的痛苦,使用类似xpatindexof的东西来完成这项工作,但不是很好,我想搜索不同的字符串执行此操作的关键是:子字符串的第一个索引是起始字符串示例:加上该字符串的长度,因为您最初得到它的第一个字符的索引。至于结束索引,只需指定结束字符串
import java.io.IOException;
import java.io.StringReader;
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.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class A {
public static void main(String[] args) throws Exception {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
String xml1 = "<xml><car_dealer><car_id>2</car_id></car_dealer><car><car_id>12345678</car_id></car></xml>";
String xml2 = "<xml><car_dealer><car_id>2</car_id></car_dealer><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>";
Document doc1 = stringToDom(xml1);
Document doc2 = stringToDom(xml2);
XPathExpression expr1 = xpath.compile("//car/car_id/text()");
String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING);
XPathExpression expr2 = xpath.compile("//car/car_type/text()");
String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING);
System.out.println("***");
System.out.println("carId: " + carId);
System.out.println("carType: " + carType);
System.out.println("***");
/* prints
***
carId: 12345678
carType: 55555
***
*/
}
public static Document stringToDom(String xmlSource) throws SAXException,
ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
}