Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法读取XML,因为XML文件中有空格_Java_Xml_Whitespace - Fatal编程技术网

Java 无法读取XML,因为XML文件中有空格

Java 无法读取XML,因为XML文件中有空格,java,xml,whitespace,Java,Xml,Whitespace,由于xml文件中存在空格,我面临一个问题。我想从xml中读取数据,但由于节点之间的空间,我面临各种问题 前 如果一个节点有4个子节点,则由于该节点显示了9个子节点的空格。 所以,当我尝试在表中显示节点值时,也会创建一些没有标题和数据的列。 当我删除这些空格时,我成功地读取了文件,没有任何问题。 那么如何解决这个问题呢?因为每次当我读取多个xml文件时,我都不能亲自从文件中删除空白。因此,从每个文件中删除空格将是一项乏味的工作 Document doc; DocumentBuilderFactor

由于xml文件中存在空格,我面临一个问题。我想从xml中读取数据,但由于节点之间的空间,我面临各种问题

如果一个节点有4个子节点,则由于该节点显示了9个子节点的空格。
所以,当我尝试在表中显示节点值时,也会创建一些没有标题和数据的列。

当我删除这些空格时,我成功地读取了文件,没有任何问题。

那么如何解决这个问题呢?因为每次当我读取多个xml文件时,我都不能亲自从文件中删除空白。因此,从每个文件中删除空格将是一项乏味的工作

Document doc;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(fp);
doc.getDocumentElement().normalize();            
NodeList nList = doc.getElementsByTagName("parameter"); 
nodeName = name; 
for (int temp = 0; temp < nList.getLength();temp++)
{
     Node nNode =  (Node) nList.item(temp);
     Element eElement = (Element) nNode;
     String upname1 = getTagValue("name", eElement);
     System.out.println(nodeName+"   UP:    "+upname1);

     if(upname1.equals(nodeName))
     {  
           NodeList pvalList = eElement.getElementsByTagName("paramvalues");
           for(int l=0; l< pvalList.getLength(); l++)
           {
                 Node pNode = (Node) pvalList.item(l);
                 NodeList valList = pNode.getChildNodes();

                 for(int j=0; j<valList.getLength(); j++)
                 {
                     Node valNode = (Node) valList.item(j);

                     if(valNode.getNodeName().equals("value"))
                     {
                          NamedNodeMap att = valNode.getAttributes();

                          for(int s=0; s<att.getLength(); s++)
                          {
                              Node n1 = att.item(s);
                              System.out.println("len:   "+att.getLength());
                              if(n1.getNodeName().equals("default"))
                                   def = n1.getNodeValue();

                              if(n1.getNodeName().equals("actualvalue"))
                                   aval = n1.getNodeValue();
                           }
                      }
                 }
           }
     }
}
文档文档;
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
doc=dBuilder.parse(fp);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“参数”);
nodeName=名称;
对于(int-temp=0;temp对于(int j=0;j您可以尝试创建一个类,在解析文档时从字符串的左侧和右侧修剪空白,例如“John Smith”变为“John Smith”| src:


您可以尝试创建一个类来修剪字符串左右两侧的空白,同时解析文档,例如“John Smith”变为“John Smith”| src:


您能显示有问题的Java代码吗?可能是因为您的代码没有检查节点是否是“元素”的实例。您可能需要过滤掉“文本节点”。这很简单,但您需要显示一些代码:)1.尝试并
normalize()
解析XML数据之前请先将其粘贴。2.请粘贴一些代码!@amir75是否还需要检查每个子节点的XML数据?是的,检查childNodes的节点列表。当您遍历getChildNodes()返回的节点列表时,空白将作为“文本”节点返回,这些节点不是元素的实例。嗯,看看代码,它并不是我期望看到的代码,但可能您只粘贴了部分代码……您能显示有问题的Java代码吗?可能是因为您的代码没有检查节点是否是“元素”的实例。您可能需要筛选ut“text nodes”。这很简单,但您需要显示一些代码:)1.在解析XML数据之前尝试并
normalize()
XML数据。2.请粘贴一些代码!@amir75我是否也必须为每个子节点检查它?是的,为childNodes的nodelist检查。当您迭代getChildNodes()返回的nodelist时,空白将作为“文本”节点返回,这些节点不是元素的实例。嗯,看看代码,它并不完全是我期望看到的代码,但也许您只是粘贴了其中的一部分。。。
 public class Trims{
   public static String ltrim(String s){
      int i = 0;
      while (i < s.length() && Character.isWhitespace(s.charAt(i))){
         i++;
      }
      return s.substring(i);
   }

   public static String rtrim(String s){
      int i = s.length() - 1;
      while (i > 0 && Character.isWhitespace(s.charAt(i))){
         i--;
      }
      return s.substring(0, i + 1);
   }

   public static void main(String[] args){
      String s = " \t \r \n String with spaces  \t  \r  \n  ";
      System.out.println(ltrim(s));
      System.out.println(rtrim(s));
      System.out.println(s.trim()); //trims both ends
   }
        // a builder takes a boolean value meaning validation mode:
        DOMBuilder builder = new DOMBuilder(false);

        // simply load the document::
        Document document = builder.build(
                new java.io.File("sample.xml"));

        Element order = document.getRootElement();

        // find an address tag:
        Element address = 
                order.getChild("purchased-by").getChild("address");