java dom解析xml
<?xmlversion="1.0"encoding="UTF-8"?><File><Head><FileVersion>010000</FileVersion><Fil...
<?xml version="1.0" encoding="UTF-8"?>
<File>
<Head>
<FileVersion>010000</FileVersion>
<FileLength>00000CC3</FileLength>
<FileTime>20100629173405</FileTime>
<KeyIndex>1</KeyIndex>
</Head>
<Body>
<HLR HlrId="0231">
<Quantity>000001</Quantity>
</HLR>
</Body>
</File>
<HLR HlrId="0231"> 这个0231怎么取到啊? 展开
<File>
<Head>
<FileVersion>010000</FileVersion>
<FileLength>00000CC3</FileLength>
<FileTime>20100629173405</FileTime>
<KeyIndex>1</KeyIndex>
</Head>
<Body>
<HLR HlrId="0231">
<Quantity>000001</Quantity>
</HLR>
</Body>
</File>
<HLR HlrId="0231"> 这个0231怎么取到啊? 展开
1个回答
展开全部
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class FileTest {
/**
* @param args
*/
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("d:/test.xml");
//得到根节点
Element root = doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("HLR");
Element e = (Element) nl.item(0);
String hlrId=e.getAttribute("HlrId");
System.out.println(hlrId);
}catch(Exception e){
e.printStackTrace();
}
}
}
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class FileTest {
/**
* @param args
*/
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("d:/test.xml");
//得到根节点
Element root = doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("HLR");
Element e = (Element) nl.item(0);
String hlrId=e.getAttribute("HlrId");
System.out.println(hlrId);
}catch(Exception e){
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询