我正在解析一个xml文件,其中有一个表如下:
<table name="categoryAttributeTable">
<row>
<field name="CID">201</field>
<field name="name">page_title</field>
<field name="value">Spotlight</field>
</row>
<row>
<field name="CID">301</field>
<field name="name">page_title</field>
<field name="value">Oryx highlights</field>
</row>
<row>
<field name="CID">501</field>
<field name="name">page_title</field>
<field name="value">Little Taster</field>
</row>
</table>
我想搜索CID 301的值,其中ans应该是Oryx的亮点,但我得到的是Spotlight作为ans,这是CID 201的值。为什么会发生这种情况? 我的java代码是:
public static void main(String argv[]) {
try {
File fXmlFile = new File("/home/media.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("table");
System.out.println("----------------------------");
String titlevalue=null;
String cidvalue=null;
String lidvalue=null;
List a = new ArrayList();
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if(eElement.getAttribute("name").equalsIgnoreCase("categoryAttributeTable"))
{
NodeList nList1 = eElement.getElementsByTagName("row");
for (int temp1 = 0; temp1 < nList1.getLength(); temp1++) {
Node nNode1 = nList1.item(temp1);
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
NodeList nList2 = eElement1.getElementsByTagName("field");
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Node nNode2 = nList2.item(temp2);
if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
Element eElement2 = (Element) nNode2;
if(eElement2.getAttribute("name").equalsIgnoreCase("value"))
{
titlevalue=eElement2.getTextContent();
// System.out.println(" Title value :: "+titlevalue);
}
if(eElement2.getAttribute("name").equalsIgnoreCase("CID") && (eElement2.getTextContent().equals(String.valueOf(301))))
{
System.out.println(" Title value :: "+titlevalue);
}
}
}
}
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
在eElement2.getAttribute("name").equalsIgnoreCase()
中,如果我传递字符串,则输出将按预期进行,但如果我将整数作为字符串传递,那么它将显示前一级别的ans。
请帮帮我......
答案 0 :(得分:0)
看到这个......
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile {public static void main(String argv[]) {
try {
File fXmlFile = new File("src/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("table");
System.out.println("----------------------------");
String titlevalue=null;
String cidvalue=null;
String lidvalue=null;
List<String> valueList=new ArrayList<String>();
List<String> cidList=new ArrayList<String>();
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if(eElement.getAttribute("name").equalsIgnoreCase("categoryAttributeTable"))
{
NodeList nList1 = eElement.getElementsByTagName("row");
for (int temp1 = 0; temp1 < nList1.getLength(); temp1++) {
Node nNode1 = nList1.item(temp1);
if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode1;
NodeList nList2 = eElement1.getElementsByTagName("field");
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Node nNode2 = nList2.item(temp2);
if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
Element eElement2 = (Element) nNode2;
String value1=null;
String value2=null;
if(eElement2.getAttribute("name").equalsIgnoreCase("value"))
{
valueList.add(eElement2.getTextContent());
// System.out.println(" Title value :: "+eElement2.getTextContent());
}
if(eElement2.getAttribute("name").equalsIgnoreCase("CID") && !(eElement2.getTextContent().equals("NULL")))
{
// System.out.println(" XXXXXXXXXXX Title value :: "+eElement2.getTextContent());
cidList.add(eElement2.getTextContent());
}
}
}
}
}
}
}
}
for(int i=0;i<valueList.size();i++)
{
System.out.println("value :: "+ valueList.get(i)+" corresponding cid :: "+ cidList.get(i));
//System.out.println("cid :: "+ cidList.get(i));
}
}
catch (Exception e) {
e.printStackTrace();
}
}}
答案 1 :(得分:0)
我只是想指出你的错误在哪里。这是您的原始代码。
if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
Element eElement2 = (Element) nNode2;
if(eElement2.getAttribute("name").equalsIgnoreCase("value")){
titlevalue=eElement2.getTextContent();
}
if(eElement2.getAttribute("name").equalsIgnoreCase("CID") && (eElement2.getTextContent().equals(String.valueOf(301)))) {
System.out.println(" Title value :: "+titlevalue);
}
}
<row>
<field name="CID">201</field>
<field name="name">page_title</field>
<field name="value">Spotlight</field>
</row>
<row>
<field name="CID">301</field>
<field name="name">page_title</field>
<field name="value">Oryx highlights</field>
</row>
name属性值的顺序为CID
,name
和value
。在row
代码的第一个循环结束时,您将设置titlevalue
= Spotlight。然后在row
代码的第二个循环的开头,您将跳过第一个if
,因为第一个字段属性等于CID
。但该程序将输入第二个if
,因为您定义了name=CID
和content=301
,因此它会显示titlevalue
= Spotlight。
试试这个。
Element eElement1 = (Element) nNode1;
NodeList nList2 = eElement1.getElementsByTagName("field");
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Node nNode2 = nList2.item(temp2);
if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
Element eElement2 = (Element) nNode2;
String nameattr = eElement2.getAttribute("name");
if (nameattr.equalsIgnoreCase("CID")) {
titlevalue = eElement2.getTextContent();
// If CID != 301, skip the whole loop.
if (!titlevalue.equalsIgnoreCase("301")) {
break;
}
}
else if (nameattr.equalsIgnoreCase("value")) {
System.out.println("value: " + eElement2.getTextContent());
}
}
}