我有下一个情况,我有一个像xml的字符串,我想搜索到这个字符串到第二个car_id(<car_id>12345678</car_id>
)并获取值
如果无法搜索car_type(<car_type id_1="2" id_2="32">55555</car_type>
)
我尝试了下面的代码,见下文,但工作不正常。是否有更好的方法来执行/循环字符串?谢谢并感谢Stackoverflow
的字符串:
<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>
代码:
String carId = input.substring(input.lastIndexOf("<car_id>")+9, input.lastIndexOf("</car_id>"))
String carType= input.substring(input.indexOf("<car><car_type>")+84, input.indexOf("</car>"))
答案 0 :(得分:1)
我强烈建议您不要使用regex或indexOf -trick来解析这些字符串。那些总是会在某个时候破裂。
如果您的字符串确实看起来像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)));
}
}
答案 1 :(得分:0)
您可以使用如下的模式匹配找到车辆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));
}
答案 2 :(得分:-1)
指数将完成这项工作:
原件:
<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>
解决方案索引:
String car_id = input.substring(input.indexOf("<car><car_id>") + "<car><car_id>".length(), input.indexOf("</car_id></car>"));
为他人做同样的事。
祝你好运!