使用Java解析ncx(xml)以更改PlayOrder

时间:2011-05-19 15:07:27

标签: java xml xml-parsing epub

可能是一个新手问题,但是,我正在尝试使用Java解析并更改一个ncx文件(在ePub格式中很重要),以确保PlayOrder属性处于正确的顺序(1,2 ,3,4 ......)。 这是我的java到目前为止,它工作得很好,但它将PlayOrder设置为(1,3,5,7 ......)。猜猜它正在解析一个标签到多()。有什么想法吗?

package com.mkyong.common;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ModifyXMLFile {

 public static void main(String argv[]) {

   try{
 String filepath = "toc.ncx";
 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

 Document doc = docBuilder.parse(filepath);


 //Get the root element
 Node company = doc.getFirstChild();

 //Get the staff element , it may not working if tag has spaces, or
     //whatever weird characters in front...it's better to use
     //getElementsByTagName() to get it directly.
 //Node staff = company.getFirstChild();

     //Get the staff element by tag name directly
     Node navPoint = doc.getElementsByTagName("navMap").item(0);

 //update staff attribute

 /*NamedNodeMap attr = navPoint.getAttributes();
 Node nodeAttr = attr.getNamedItem("navPoint");

 nodeAttr.setTextContent("2");*/

 //append a new node to staff
 /*Element age = doc.createElement("age");
 age.appendChild(doc.createTextNode("28"));
 staff.appendChild(age);*/

 //loop the staff child node
 NodeList list = navPoint.getChildNodes();

 for (int i =0; i<list.getLength();i++){

     Node node = list.item(i);




     //get the salary element, and update the value

             NamedNodeMap attr = node.getAttributes();

            if(attr != null){
             Node nodeAttr = attr.getNamedItem("playOrder");
               System.out.println(nodeAttr);

             String aString = Integer.toString(i);
             nodeAttr.setTextContent(aString);
              }

 }

 //write the content into xml file
 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 Transformer transformer = transformerFactory.newTransformer();
 DOMSource source = new DOMSource(doc);
 StreamResult result =  new StreamResult(new File(filepath));
 transformer.transform(source, result);

 System.out.println("Done");

   }catch(ParserConfigurationException pce){
 pce.printStackTrace();
   }catch(TransformerException tfe){
 tfe.printStackTrace();
   }catch(IOException ioe){
 ioe.printStackTrace();
   }catch(SAXException sae){
 sae.printStackTrace();
   }
 }
}

1 个答案:

答案 0 :(得分:0)

我的猜测:navPoint.getChildNodes()获得的不仅仅是navPoint元素,Oxygen中的XPath“/ ncx / navMap / node()”还会产生novPoint元素之间的(不可见)文本节点。 因为它们没有属性,if(attr != null)会跳过它们,因此只处理每个第二个节点,因此只使用i的每一个值。

是否有getChildElements()

致以最诚挚的问候,

基督教