使用java获取字符串中特定值的字符串操作
我有一个字符串如下使用java获取字符串中特定值的字符串操作,java,Java,我有一个字符串如下 <employees> <emp> <name>yaakobu</name> <sal>$20000</sal> <designation>Manager</designation> </emp> <emp> <name>daaniyelu</name> <sal>$2000</sal> <design
<employees>
<emp>
<name>yaakobu</name>
<sal>$20000</sal>
<designation>Manager</designation>
</emp>
<emp>
<name>daaniyelu</name>
<sal>$2000</sal>
<designation>Operator</designation>
</emp>
<emp>
<name>paadam</name>
<sal>$7000</sal>
<designation>Engineer</designation>
</emp>
</employees>
雅可布
$20000
经理
达尼耶鲁
$2000
操作人员
帕亚当
$7000
工程师
上面的xml是作为字符串获取的。由于性能问题,我被要求不要使用解析。我需要使用java的字符串操作获取第二名员工的工资($2000)。请提供一些指针
感谢您的帮助。您的字符串是xml。
尽管使用正则表达式或其他字符串操作从xml中提取数据很有诱惑力,但不要这样做,这是一种糟糕的做法
您应该改用一些XML解析器。您的字符串是XML。
尽管使用正则表达式或其他字符串操作从xml中提取数据很有诱惑力,但不要这样做,这是一种糟糕的做法
您应该改用一些XML解析器。我怀疑使用
XML
解析器会有性能问题,但是如果您想通过字符串解析来完成,请使用str.indexOf(“”,str.indexOf(“”)+5)代码>那么就很容易了。我怀疑使用xml
解析器会有性能问题,但是如果您想通过字符串解析来完成,请使用str.indexOf(“”,str.indexOf(“”)+5)代码>那么就很容易了。您可以使用xstream
将xml放入对象结构中并从中获取
检查样品,很容易使用
如果您不想解析自己…:)您可以使用xstream
将xml放入对象结构中并从中获取
检查样品,很容易使用
如果您不想自己解析…:)使用xml解析器或JAXB api将字符串解组到对象中,您也可以通过这种方式来完成
private static Object getObject(String yourXml) throws Exception {
JAXBContext jcUnmarshal = null;
Unmarshaller unmarshal = null;
javax.xml.stream.XMLStreamReader rdr = null;
//Object obj = null;
try {
jcUnmarshal = JAXBContext.newInstance("com.test.dto");
unmarshal = jcUnmarshal.createUnmarshaller();
rdr = javax.xml.stream.XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(yourXml));
//obj = (Object) unmarshal.unmarshal(rdr);
return (Object) unmarshal.unmarshal(rdr);
} catch (JAXBException jaxbException) {
jaxbException.printStackTrace();
log.error(jaxbException);
throw new ServiceException(jaxbException.getMessage());
}
finally{
jcUnmarshal = null;
unmarshal = null;
rdr.close();
rdr = null;
}
//return obj;
}
使用xml解析器或JAXB api将字符串解组到对象中,您也可以通过这种方式来完成
private static Object getObject(String yourXml) throws Exception {
JAXBContext jcUnmarshal = null;
Unmarshaller unmarshal = null;
javax.xml.stream.XMLStreamReader rdr = null;
//Object obj = null;
try {
jcUnmarshal = JAXBContext.newInstance("com.test.dto");
unmarshal = jcUnmarshal.createUnmarshaller();
rdr = javax.xml.stream.XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(yourXml));
//obj = (Object) unmarshal.unmarshal(rdr);
return (Object) unmarshal.unmarshal(rdr);
} catch (JAXBException jaxbException) {
jaxbException.printStackTrace();
log.error(jaxbException);
throw new ServiceException(jaxbException.getMessage());
}
finally{
jcUnmarshal = null;
unmarshal = null;
rdr.close();
rdr = null;
}
//return obj;
}
使用字符串操作完成此操作后,请尝试以下操作:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("test.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
// get the salary from the employee at index 1
XPathExpression expr = xpath.compile("//emp[1]/sal");
Object salary = expr.evaluate(doc, XPathConstants.STRING);
System.out.println(salary);
}
}
应输出:
$20000
我不能保证它会更快,但我认为不会有太大的不同。这样做比使用indexOf(…)
和子字符串(…)
调用要容易得多。使用字符串操作完成此操作后,请尝试以下操作:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("test.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
// get the salary from the employee at index 1
XPathExpression expr = xpath.compile("//emp[1]/sal");
Object salary = expr.evaluate(doc, XPathConstants.STRING);
System.out.println(salary);
}
}
应输出:
$20000
我不能保证它会更快,但我认为不会有太大的不同。这样做比使用indexOf(…)
和子字符串(…)
调用要容易得多。AFAIK使用xml解析器不会降低性能。如果我错了,请通知我。“由于性能问题而不使用解析”什么都没有意义!是否通过输入中员工的姓名或职位查找XML节点?使用XML解析器的AFAIK不会降低性能。如果我错了,请通知我。“由于性能问题而不使用解析”什么都没有意义!您是否通过输入中员工的姓名或职位查找XML节点?@Bart Kiers-您是对的,我的帖子只是OP的开始。@Bart Kiers-您是对的,我的帖子只是OP的开始。